home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.compilers
- Path: sparky!uunet!think.com!spdcc!iecc!compilers-sender
- From: Christopher Traynor<traynorc@Phibro.COM>
- Subject: Alignment of structures
- Reply-To: Christopher Traynor<traynorc@Phibro.COM>
- Organization: Compilers Central
- Date: Wed, 18 Nov 1992 14:11:29 GMT
- Approved: compilers@iecc.cambridge.ma.us
- Message-ID: <92-11-101@comp.compilers>
- Keywords: C, question, storage, comment
- Sender: compilers-sender@iecc.cambridge.ma.us
- Lines: 36
-
- Could anyone tell me how the structure alignment mechanism works
- in gcc? Specifically, I am interested in knowing how to manipulate the
- compiler to produce the smallest, but most efficient ending size and
- alignment when a declaration like the following is being made:
-
- struct test_struct
- {
- double dDouble ;
- short siShort ;
- char szChar[10] ;
- int iInt ;
- double dAnotherDouble ;
- } ;
-
- I have heard that a significant amount of padding is used to align
- the struct against the first type - here the double. This would cause a
- lot of waste. Is it really the case that you have to declare the elements
- in an order that is aware of this fact and by its nature reduces the
- padding in effect? I need to know this because in the work that I am
- doing I am allocating large amounts of memory and mapping structures into
- it - I cannot afford to waste all of this space.
-
- Thanks in advance for your responses...
-
- Cheers,
- Chris
- [The C standard requires that structure elements be allocated in the order
- you declare them so that hacks with structures with common first parts work.
- Some C compilers, particularly on machines that don't require operand
- alignment, have ``#pragma pack'' or the like to tell it not to leave holes.
- Usually if you put the fields largest first you can avoid padding, but if
- you really want to save space there's still no substitute for knowing how
- your particular computer works. -John]
- --
- Send compilers articles to compilers@iecc.cambridge.ma.us or
- {ima | spdcc | world}!iecc!compilers. Meta-mail to compilers-request.
-