The following code:-
I would expect to output 20 . double is 8 bytes*2=16 + 4 forCode:class Box { public: int height; double length; double breadth; }; cout << sizeof(Box);
an int =20 but the result i am getting is 24 . Can anyone explain why?
The following code:-
I would expect to output 20 . double is 8 bytes*2=16 + 4 forCode:class Box { public: int height; double length; double breadth; }; cout << sizeof(Box);
an int =20 but the result i am getting is 24 . Can anyone explain why?
because a class has a function pointer table. 4 bytes = long doesn't it? I think it does. so the long is a pointer to the function pointer table. Can anyone verify that?
> Can anyone explain why?
Structures are padded to the alignment of the most restrictive type within your structure.
Your struct contains a double, so the whole struct needs to be a multiple of 8 in size (I guess from your figures). If you try with ints and chars, it will probably be rounded up to the next multiple of 4 say.
This is so that if you have an array of them (for example), all the members of the structure will be accessed efficiently, no matter which array element they are in.
I'm pretty sure that no matter what you put in a class, you have an extra 4 bytes. I've run tests on this because I saw the same thing. Try it out by changing what's in there.
Let me get this straight so the moment you have a double in
your class the byte size must be divisable by 8 . Is that right?
> so the moment you have a double in
> your class the byte size must be divisable by 8
If the alignment of doubles on your machine is 8, then yes.
doubles are already 8 bytes in size, so a class containing only doubles would be a multiple of 8 anyway.
this is wierd. I ran tests with chars and longs in a class. When there was just 3 chars in the class sizeof() returned 3. When there was a long, sizeof returned 4. so far makes sense. But when there was a long and an char, sizeof returned 8.
Salem, how come it doesn't round up when there are just chars?
does the this pointer present in each class (whether you want it or not) thow off the "expected" results by 4-- the size of a pointer?
I don't think the "this" pointer would make any sense to be stored in the structure. You have to be accessing the memory already to get it so you would have the address already. my head is spinning, thank you
> Salem, how come it doesn't round up when there are just chars?
The alignment of char is always 1, so there is no need to pack chars to any particular boundary.
> But when there was a long and an char, sizeof returned 8.
For you, I'd say the alignment of long is 4
Since you have 5 bytes already (sizeof(long)+sizeof(char)), the next multiple will be 8
Consider these two alternatives for arranging a char and a long
Code:#include <iostream.h> #include <stddef.h> struct foo { char a; long b; }; struct bar { long a; char b; }; int main ( ) { cout << offsetof(foo,a) << " " << offsetof(foo,b) << endl; cout << offsetof(bar,a) << " " << offsetof(bar,b) << endl; return 0; }
Alignment is processor dependant I believe. If you look in some ms header files you will see
#pragma pack( 8 )
So, obviously they are using 8-byte alignment. If you want to see the output as 20 bytes instead of 24, you should use the pragma directive and specify 4 byte alignment. This is really only necessary on palm pilot software and other memory restrictive environments.