linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Alignment of variables in gcc
@ 2002-11-23 23:42 Reinaldo Nolasco Sanches
  2002-11-23 23:51 ` Jason P. Winters
  0 siblings, 1 reply; 3+ messages in thread
From: Reinaldo Nolasco Sanches @ 2002-11-23 23:42 UTC (permalink / raw)
  To: linux-c-programming


Sorry for this question here... but anybody have any good doc with explain this ???

i.e
In a struct is I put 

typedef struct myStruct {
  char variable1;
  int variable2;
} myStruct;

The sizeof() myStruct is 8, I read some documents and see with this not a bug, this occurs because
"Alignment requeriments"...

Anybody can explais this or have any documents for a newbie... I need of a great explanation with
images and text :)

Sorry for my bad english too...

Thanks in Advance... and not hate me.



=====
"When you know Slackware, you know Linux... when you know Red Hat, all you know is Red hat"

- I haven't lost my mind -- it's backed up on tape somewhere.
- Power doesn't corrupt people, people corrupt power.

- r_linux@yahoo.com -- http://slackware.linuxbr.org
- UIN: 42853394 - irc.brasnet.org(#slackware)

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus – Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Alignment of variables in gcc
  2002-11-23 23:42 Alignment of variables in gcc Reinaldo Nolasco Sanches
@ 2002-11-23 23:51 ` Jason P. Winters
  2002-11-24  7:34   ` Glynn Clements
  0 siblings, 1 reply; 3+ messages in thread
From: Jason P. Winters @ 2002-11-23 23:51 UTC (permalink / raw)
  To: Reinaldo Nolasco Sanches; +Cc: linux-c-programming

 
> Sorry for this question here... but anybody have any good doc with explain this ???

No problem..  For me, anyway!

> i.e
> In a struct is I put 
> 
> typedef struct myStruct {
>   char variable1;
>   int variable2;
> } myStruct;
> 
> The sizeof() myStruct is 8, I read some documents and see with this not a bug, this occurs because
> "Alignment requeriments"...

Yep.  This is not a bug...  It has to do with the _hardware_ requirements of the CPU, and speed
accessing memory...

With a 32 bit processor, all 'words' must be accessed on a 'word' boundry for maximum speed...
I.e., when the least significant two bits of the address are 0.

So, the structure starts out 'word' aligned.  For example, at address 0x100.  Your character
is at 0x100, so the next available address is 0x101.  Now, you specified an 'int', which must
be word aligned... so, the compiler moves to the next word aligned boundry, which happens to
be 0x104. (lower two bits are zero). 
Then it allocates 4 bytes for your int.  And thus, you have 8 bytes used in sizeof(struct),
but are really only using 5 of them.

If you swap the int and char definitions, you'll probably end up
with what you think is the right size.. i.e.,

typedef struct myStruct {
   int variable2;
   char variable1;
} myStruct;

Will probably (but not *always*) yeild you sizeof = 5.

Okay?

Ciao!

Jason
-- 
|UUCP  jason@txt.com                        Who wills, Can.
|VOICE (408) 243-3425                         Who tries, Does.
|LOCAL Hey, Jason!                              Who loves, Lives.          o_.
|Disclaimer:  Not me! I didn't do *THAT!*                                 <|
|Local Disclaimer:  I'm not here!                   A. McCaffrey           4

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Alignment of variables in gcc
  2002-11-23 23:51 ` Jason P. Winters
@ 2002-11-24  7:34   ` Glynn Clements
  0 siblings, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2002-11-24  7:34 UTC (permalink / raw)
  To: Jason P. Winters; +Cc: Reinaldo Nolasco Sanches, linux-c-programming


Jason P. Winters wrote:

> > i.e
> > In a struct is I put 
> > 
> > typedef struct myStruct {
> >   char variable1;
> >   int variable2;
> > } myStruct;
> > 
> > The sizeof() myStruct is 8, I read some documents and see with this not a bug, this occurs because
> > "Alignment requeriments"...
> 
> Yep. This is not a bug... It has to do with the _hardware_
> requirements of the CPU, and speed accessing memory...
> 
> With a 32 bit processor, all 'words' must be accessed on a 'word'
> boundry for maximum speed... I.e., when the least significant two
> bits of the address are 0.

Actually, on many architectures, it isn't just a performance issue;
words must be aligned otherwise either an exception occurs or you get
the wrong data. x86 is one of the few architectures where the CPU
allows unaligned accesses.

> If you swap the int and char definitions, you'll probably end up
> with what you think is the right size.. i.e.,
> 
> typedef struct myStruct {
>    int variable2;
>    char variable1;
> } myStruct;
> 
> Will probably (but not *always*) yeild you sizeof = 5.

No it won't. The size of a structure needs to be a multiple of the
largest alignment of any of its members; otherwise you would have
problems with arrays.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-11-24  7:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-11-23 23:42 Alignment of variables in gcc Reinaldo Nolasco Sanches
2002-11-23 23:51 ` Jason P. Winters
2002-11-24  7:34   ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).