-
Strange size of struct
Hi there,
I have a question regarding structs, I have a struct with five members, 2 bytes + 4 bytes + 2 bytes + 2 bytes + 4 bytes, so this should mean my struct is 14 bytes large, the sizes of the members are correct, if I do a sizeof(member) then I get these numbers, however sizeof(my struct) is 16 bytes large, how come?
Thanks in advance,
Bill
Update:
Ok, if I do
Code:
struct test {
unsigned short mem1;
}
Then my struct is 2 bytes large, which is fine.
If I do
Code:
struct test {
unsigned short mem1;
unsigned long mem2;
}
then my struct is 8 bytes large, even though (unsigned long) is only 4 bytes, I am confused :(
Somehow only the last long will get a 6 byte size, does anyone know why?
I mean I have more than one struct in my program, and the others do have correct sizes.
-
Search the Web for phrases like "struct padding" and "data structure alignment".
-
Cool thank you, I got it working with the pragma directive. :)
-
Well, before you get too excited about your solution, remember that there is reason for the padding in the first place, so it may not be desirable to pack the struct such that it is not padded, unless this packing is really necessary.
-