[LCP]struct size

Bill Rausch bill at numerical.com
Wed Aug 22 03:02:04 UTC 2001


At 9:17 PM +0200 8/18/01, Roberto Diaz wrote:
>  > please see this code
>>  struct somestruct
>>  {
>>   int a;
>>   char b;
>>   char c;
>>  }stvar;
>>  int main()
>>  {
>>    printf("%d",sizeof stvar); /* you dont need () since it is an instance*/
>>  }
>>  the o/p is 8.  i was expecting 6 as 'a' occupies 4 bytest and 'b','c'
>>  one byte each !  how is it 8 ??
>
>Aligment.. the word size in i386 is 32 bits 4 bytes so the compiler left
>holes into your structure so all is aligned at 4 bytes boundaries.
>

Note that 8 bytes total size doesn't match 32 bit alignment.  You'd 
have had 12 bytes total size then.  It does match what would happen 
with 16 bit alignment.

Various CPUs/compilers are capable of byte addressing or not. 
Apparently you are using one that is capable of addressing each pair 
of bytes.

-- 
  Bill Rausch, Software Development, Unix, Mac, Windows
  Numerical Applications, Inc.  509-943-0861   bill at numerical.com



More information about the linuxCprogramming mailing list