home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / compiler / 1910 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.0 KB  |  50 lines

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