C and Sizeof question..
Ben Duncan
bend at linux4ms.net
Wed Nov 12 13:21:38 PST 2008
Righto .. also KNOW as BYTE alignment ....
GOOGLE Byte Alignment and Structure .....
Bill Campbell wrote:
> On Wed, Nov 12, 2008, Steve Jardine wrote:
>> Calling all C Gurus!!
>>
>> Consider this C code:
>>
>> #include <stdio.h>
>> main() {
>>
>> typedef struct {
>> char name[256];
>> char type[1];
>> int size;
>> }resp;
>>
>> char n[256];
>> char t[1];
>>
>> printf("sizeof resp is %d\n",sizeof(resp));
>> printf("sizeof int is %d\n",sizeof(int));
>> printf("Total: %d\n", sizeof(int)+sizeof(n)+sizeof(t));
>> }
>>
>> Can anyone explain why the sizeof the struct is 264 bytes, yet adding up all of its components yields 261 bytes?
>> Call me confused...
>
> Confused:
>
> This is likely because structure elements are being forced to modulo 4
> addresses.
>
> I first ran into this problem about 20 years ago porting Unify RDBMS code
> from systems with Motorola MC68xxx bigendian processors to SCO Xenix and
> Unix systems on little endian systems.
>
> Bill
--
Ben Duncan - Business Network Solutions, Inc. 336 Elton Road Jackson MS, 39212
"Never attribute to malice, that which can be adequately explained by stupidity"
- Hanlon's Razor
More information about the Linux-users
mailing list