On Mon Oct 03, 2005 at 19:44:44 -0500, [EMAIL PROTECTED] wrote:
>
>>#include <stdio.h>
>>struct verify {
>>char initials[2];
>>int birthdate;
>>};
>>int main(void)
>>{
>>struct verify holes;
>>printf ("%d\n", sizeof(holes.initials[0]));
>>printf ("%d\n", sizeof(holes.initials));
>>printf ("%d\n", sizeof(holes.birthdate));
>>printf ("%d\n", sizeof(holes));
>>return 0;
>>}
>>Given that the word-byte
>>In 16-bit computer = 2 bytes word the output is,
>>1
>>2
>>2
>>4
>>in 32-bit computer = 4 bytes word the output is,
>>1
>>2
>>4
>>8
>
>
>k, I am a newb, so someone plz quickly explain to me why the variable
>'initial'
>takes 2 bytes, 'birthdate' takes 4 bytes but the struct which is 2+4 = 6
>bytes
>takes 8 bytes?
>
See my last email, but it is likely that in memory it is layed out as:
[ char(1) | char(1) | padding(2) | int(4) ]
So there is two bytes of padding after the characters to make sure the integer
is aligned. Having the integer aligned makes for a faster program (in general),
at the expense of a nominal (in general) amount of memory.
The compiler has done a space<->time trade off on your behalf.
Benno
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html