C Programming help .....wtf ?!?!

Roger Oberholtzer roger
Wed Jan 26 02:36:23 PST 2005


On Tue, 2005-01-25 at 21:30, Vu Pham wrote:
> > -----Original Message-----
> > From: linux-users-bounces at linux-sxs.org 
> > [mailto:linux-users-bounces at linux-sxs.org] On Behalf Of Ben Duncan
> > Sent: Tuesday, January 25, 2005 2:16 PM
> > To: Linux tips and tricks
> > Subject: Re: C Programming help .....wtf ?!?!
> > 
> > Why does the sizeof( struct _dummymenu) on the below return 8 ?
> > 
> > 
> >    typedef struct _dummymenu
> >         {
> >         char d_char ;
> >         int d_sumnumber ;
> >         } DummyMenu ;
> > 
> > Yet if I do one a structure like this:
> > 
> > typedef struct _dummymenu
> >         {
> >         char a_char ;
> >         char b_char ;
> >         char c_char ;
> >         } DummyMenu ;
> > 
> > it returns 3 ......
> > 
> 
> I think because of the memory aligmnet option of the compiler. You can try
> #pragma pack(n) to change the default alignment, like

Of course. I go for this as well (over my eariler pre-coffee dribble) .
I forgot. We use this for data structures that we send over the net to
machines running other OS. Like OS/9. Keep in mind that you should only
pack when needed as the default behavior results in faster access to the
parts. Especially on platforms that do, say, 32-bit access no matter.
Then the compiler has to mask and shift the data as well. This is
another reason to use the sizeof() operator and not guess the size.

> 
> #pragma pack(1)	// set the alignment to 1, all vars are adjacent to each
> other
> typedef struct _dummymenu
> {
>         char d_char ;
>          int d_sumnumber ;
> } DummyMenu ;
> 
> 
> Vu
> 
> _______________________________________________
> Linux-users mailing list
> Linux-users at linux-sxs.org
> http://mail.linux-sxs.org/cgi-bin/mailman/listinfo/linux-users
> 
> Need to chat further on this subject? Check out #linux-users on irc.linux-sxs.org !
+????????????????????????????+???????????????????????????????+
? Roger Oberholtzer          ?   E-mail: roger at opq.se        ?
? OPQ Systems AB             ?      WWW: http://www.opq.se/  ?
? Nybrogatan 66 nb           ?    Phone: Int + 46 8   314223 ?
? 114 41 Stockholm           ?   Mobile: Int + 46 733 621657 ?
? Sweden                     ?      Fax: Int + 46 8   314223 ?
+????????????????????????????+???????????????????????????????+



More information about the Linux-users mailing list