To calculate the sizes of user-defined types, the compiler takes into account any alignment space needed for complex user-defined data structures. This is why the size of a structure in C can be greater than the sum of the sizes of its members. For example, on many systems, the following code will print 8:
refer http://en.wikipedia.org/wiki/Sizeof
Suppose you have the following structure:
struct A1
{
char a[2];
int b;
};
You could think that sizeof(A1) equates to 6, but it doesn’t. It equates to 8. The compiler inserts 2 dummy bytes between members ‘a’ and ‘b’.
The reason is that the compiler will align member variables to a multiple of the pack size or a multiple of the type size, whichever is smallest.
The default pack size in visual studio is 8 bytes.
‘b’ is of the integer type, which is 4 bytes wide. ‘b’ will be aligned to the minimum of those 2, which is 4 bytes. It doesn’t matter if ‘a’ is 1, 2, 3 or 4 bytes wide. ‘b’ will always be aligned on the same address.
refer for more details