Alignment of structures

Christopher Traynor<traynorc@Phibro.COM>
Wed, 18 Nov 1992 14:11:29 GMT

          From comp.compilers

Related articles
Alignment of structures traynorc@Phibro.COM (Christopher Traynor) (1992-11-18)
| List of all articles for this month |

Newsgroups: comp.compilers
From: Christopher Traynor<traynorc@Phibro.COM>
Organization: Compilers Central
Date: Wed, 18 Nov 1992 14:11:29 GMT
Keywords: C, question, storage, comment

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]
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.