C Programming help .....wtf ?!?!
Roger Oberholtzer
roger
Sat Jan 29 04:05:12 PST 2005
As a related point, X programmers use a macro called (IIRC) OFFSET(x)
that tells where item 'x' is relative to the start of the structure. It
is most often used when initializing a number of items in a data
structure from some list of initializers - something X does more than
just about any environment I have come across. So, if you are doing lots
of initializing, this could be a useful method to add to your toolbox. I
personally have stayed away from it. But that is just me. Just check
some X code (maybe Xt) for examples.
On Wed, 2005-01-26 at 09:22 -0500, Rick Bowers wrote:
> Then again, one way to decrease the size of structures such as this is to
> order them so that the larger elements are at the top. This will eliminate
> much of the padding the compiler inserts.
>
> typedef struct _dummymenu
> {
> int d_sumnumber ;
> char d_char ;
> } DummyMenu ;
>
> ~Rick
>
> At 02:43 AM 1/26/2005, you wrote:
> >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 ?
> >+????????????????????????????+???????????????????????????????+
> >
> >_______________________________________________
> >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 !
>
> _______________________________________________
> 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 !
>
More information about the Linux-users
mailing list