1

In GCC, I need to use __attribute__((packed)) to make structs take the least amount of space, for example, if I have a large array of structs I should pack them. What other common compilers do struct padding, and how do I pack structs in these other compilers?

  • 6
    Generally all compilers use padding and you should refer to the documentation for the specific compiler you're using. I would generally disagree with your assertion that you should pack structs. Organize the members the best you can to take advantage of natural alignment and realize that if there is padding it serves a purpose. – Retired Ninja Jul 06 '20 at 02:04
  • 2
    Don't worry about attributes. Instead follow [The Lost Art Of Structure Packing](http://www.catb.org/esr/structure-packing/). – dbush Jul 06 '20 at 02:07
  • @RetiredNinja I am not saying that you should pack structs in general. I am saying it would be beneficial in certain circumstances. –  Jul 06 '20 at 02:10
  • For the MSVC there this is the relevant question: https://stackoverflow.com/questions/1537964/visual-c-equivalent-of-gccs-attribute-packed – isrnick Jul 06 '20 at 02:14
  • Please read and understand this Q/A https://stackoverflow.com/a/46790815/918959 - every time you use `__attribute__((packed))` you're subjecting yourself to all kinds of nasty phenomena if your structures have arrays that you pass around in functions, or if you pass pointers to members to functions... – Antti Haapala -- Слава Україні Jul 06 '20 at 05:12

1 Answers1

7

The premise of your question is mistaken. Packed structs are not something you're "supposed to use" to save space. They're a dubious nonstandard feature with lots of problems that should be avoided whenever possible. (Ultimately it's always possible to avoid them, but some people balk at the tradeoffs involved.) For example, whenever you use packed structures, any use of pointers to members is potentially unsafe because the pointer value is not necessarily a valid (properly aligned) pointer to the type it points to. The only time there is a "need" for packed structures is when you're using them to access memory-mapped hardware registers that are misaligned, or to access in-file/on-disk data structures that are misaligned (but the latter won't be portable anyway since representation/endianness may not match, and both problems are solved together much better with a proper serialization/deserialization function).

If your goal is to save space, then as long as you control the definition of the structure, simply order it so as not to leave unnecessary padding space. This can be achieved simply by ordering the members in order of decreasing size; if you do that, then on any reasonable implementation, the wasted space will be at most the difference between the size of the largest member and the size of the smallest.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Ok thanks, but I never said that they should be avoided whenever possible. I only said that there are certain circumstances when one should avoid one. –  Jul 06 '20 at 02:36