Behaviour of sizeof in VC++.NET !

Hello All !
I have a concern about sizeof behaviour in VC++.NET. By reading the MSDN documentation I came to know that in creating classes and structs some padding is done in allocating space to members. So when I will get the sizeof following class
class MyClass
{
char _mcData;
long _mlData;
double _mdData;
};
by using cout << sizeof(MyClass);
I will get 16 which is the sizeof this class after padding. By experimenting in different scenarios I came to know that the padding is done in multiples of the largest type in the class or struct. My concern is why is such a need of padding or if there is really some reason to do this what might be the reason, if any one knows please tell me.
Regards,
Haroon Saeed
[774 byte] By [Haroon_S] at [2007-12-16]
# 1
Hi Haroon,

padding is used to use the CPU more efficiently. A 32-bit CPU can address 32 bit values quite efficient. So I assume that the padding in this case will add 24 bits to the 8-bit char to align the long at the next 32 bit offset. No additional padding needed here and the double uses 64 bits which sums up to the resulting 16 bytes (4 + 4 + 8).

So it is not the largest type in a struct/class which drives the padding but the CPU architecture and the compiler builders knowledge how to use that architecture efficiently.

HTH,
SvenC

SvenC at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...