C and Sizeof question..
Bill Campbell
linux-sxs at celestial.com
Wed Nov 12 12:41:15 PST 2008
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
--
INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
Voice: (206) 236-1676 Mercer Island, WA 98040-0820
Fax: (206) 232-9186
The fact is that the Constitution was intended to protect us from
the government, and we cannot expect the government to enforce it
willingly -- Dave E. Hoffmann, Reason Magazine March 2002
More information about the Linux-users
mailing list