473,513 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size of a class with no member

16 New Member
Can anybody tell me why the sizeof() operation on a class having no member gives a size of 1 byte.

Thanks,
Dec 8 '06 #1
7 2060
DeMan
1,806 Top Contributor
because it exists
Dec 8 '06 #2
saby
16 New Member
When I take sizeof() of a class having one integer member, that comes out as 4 bytes, but not 4+1 or 5 bytes. since integer is of 4 bytes, and a blank class of 1 byte.

Can I get some explaination please.
Dec 8 '06 #3
willakawill
1,646 Top Contributor
When I take sizeof() of a class having one integer member, that comes out as 4 bytes, but not 4+1 or 5 bytes. since integer is of 4 bytes, and a blank class of 1 byte.

Can I get some explaination please.
You might like to read this article
Dec 8 '06 #4
thefarmer
55 New Member
Can anybody tell me why the sizeof() operation on a class having no member gives a size of 1 byte.

Thanks,
hi there,

sizeof()
This operator accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object:

a = sizeof (char);

This will assign the value 1 to a because char is a one-byte long type.
The value returned by sizeof is a constant, so it is always determined before program execution.

i hope this helps you alot

regards,
Dec 8 '06 #5
horace1
1,510 Recognized Expert Top Contributor
Can anybody tell me why the sizeof() operation on a class having no member gives a size of 1 byte.

Thanks,
see the discussion on
http://www.velocityreviews.com/forums/t279653-sizeofobject-is-different-in-ansi-and-unicode.html
Dec 8 '06 #6
Banfa
9,065 Recognized Expert Moderator Expert
If you can be bothered to read the links basically a class or structure must have size, if it did not have size then in and array of such structures every entry would have the same address and the standards specifically prohibit this.

Therefore to meet the standards the compiler will assign a structure or class with no members a size of 1 so that each is guaranteed to have a different memory address to any other variable or structure. This byte is not used for anything so if a structure has data members it is not added as that would just waste space.

Note not all compilers act in the same way in relation to this problem.
Dec 8 '06 #7
saby
16 New Member
Thanx a ton Banfa. Ur explanation is really very practical.


If you can be bothered to read the links basically a class or structure must have size, if it did not have size then in and array of such structures every entry would have the same address and the standards specifically prohibit this.

Therefore to meet the standards the compiler will assign a structure or class with no members a size of 1 so that each is guaranteed to have a different memory address to any other variable or structure. This byte is not used for anything so if a structure has data members it is not added as that would just waste space.

Note not all compilers act in the same way in relation to this problem.
Dec 11 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2472
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
12
2400
by: MacFly | last post by:
Hi everyone, HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) I want that method to be class member method so it could have access to class...
18
2917
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
3
1942
by: xuatla | last post by:
Hi, I have a problem about using a class member function as a parameter in another function. What I tried to implement is something like described below: class A { public:
13
2710
by: jt | last post by:
Being a newbie in C++ and comming from C, I can't find information on how to access a class member function outside its class. Below is a snippet of the class and its member function: look at...
4
2382
by: Steve Goldman | last post by:
Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes: I'd like to assign a reference to an arbitrary...
5
1488
by: palani12kumar | last post by:
hi everybody..... i have a problem in initializing class member variables... im writing a program to create a purchase bill. i want to maintain a menu of items which should be initialized first...
7
1498
by: shaunb | last post by:
Hi. I am, hopefully, coming towards the end of my program, just need a little bit of help with this runtime error on my program: This appears when i run the debugger, appears when i get to...
2
1886
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL);...
5
1875
emaghero
by: emaghero | last post by:
I have the following class declaration class class_name{ public: class_name(); // Constructor // Member functions void function_1(); void function_2(double *mat); void...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7111
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.