C and Sizeof question..

Steve Jardine sjardine at acm.org
Wed Nov 12 13:04:40 PST 2008


Crud! I forgot about the word alignment.. 

Thx..

Steve

On Wed, 12 Nov 2008 12:41:15 -0800
Bill Campbell <linux-sxs at celestial.com> 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
> -- 
> 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
> _______________________________________________
> Linux-users mailing list ( Linux-users at linux-sxs.org )
> Unsub/Password/Etc: 
> http://linux-sxs.org/mailman/listinfo/linux-users
> 
> Need to chat further on this subject? Check out #linux-users on irc.linux-sxs.org !
> 


-- 

   Steve Jardine

Sharing the benefits of "excessive profits" of publicly traded companies 
through bourgeois Capitalism (investment). 

I'm waiting for some obscure research to say we have to stop human flatulence or global warming is going to get rampant !

"Giving money and power to government is like giving whiskey and car keys to teenage boys". -- Geoff Metcalf

"Null, void, invalid, iniquitous, unjust, damnable, reprobate, inane, and empty of meaning for all time." 
-- Pope Innocent X regarding the Treaty of Westphalia



More information about the Linux-users mailing list