0

Take the following structs, compiled with gcc 64bit

struct A {
  char a;          // size 1
  // 7 bytes padding
  long long int i; // size 8
  char b;          // size 1
  // 7 bytes padding
}; // sizeof(struct A) = 24

struct B {
  char a;          // size 1
  char b;          // size 1
  // 6 bytes padding
  long long int i; // size 8
}; // sizeof(struct B) = 16

struct __attribute__((packed)) C {
  char a;          // size 1
  long long int i; // size 8
  char b;          // size 1
}; // sizeof(struct C) = 10

All of these structs contain the same types. A is the largest. B reorders its content to achieve a smaller size. C is even smaller, but it sacrifices optimal memory alignment for that size.

Obviously memory alignment is preferred when performing most operations, but having a smaller size is also nice. B picks the best of both worlds. Going from A to B in this example is easy, but it becomes a bit bothersome to work out with bigger structs.

My question is, is there an attribute (or something else that does the same) that could be added to A which optimizes size by reordering its content without sacrificing alignment like __attribute__((packed)) does?

Sander
  • 325
  • 2
  • 8
  • 2
    The TL;DR: No. Except for squeezing out padding like the packed attribute does, compilers don't generally reorder elements to optimize/minimize padding. The usual solution is to control that _explicitly_ yourself by adding your own (e.g.) `char pad_[6];` and define the struct with the ordering you want. – Craig Estey Dec 15 '21 at 22:06
  • 1
    Luckily there is none. There is already enough chaos with padding. – Eugene Sh. Dec 15 '21 at 22:11
  • 1
    Although there is no compiler feature for this in common compilers, I do not see any reason it could not be done fairly easily. When we do not want to use a structure for serialization and do not care how it is laid out in memory, so we just want it to be a collection of members with no particular order, it makes sense to let the compiler optimize it. – Eric Postpischil Dec 15 '21 at 22:19
  • See [this footnote](http://www.c-faq.com/struct/align.esr.html) to [question 2.12](http://www.c-faq.com/struct/padding.html) in the [C FAQ list](http://www.c-faq.com/) for some guidelines on arranging things yourself. (But no, the compiler is never going to rearrange structure members for you — although Eric's right, theoretically it certainly could.) – Steve Summit Dec 15 '21 at 22:20
  • @EricPostpischil if you want it use C# or similar language where actual data representation is hidden. C does not manage the data it is against its philosophy – 0___________ Dec 15 '21 at 22:35
  • [Approach for automatic fields reordering in C-like structs](https://stackoverflow.com/q/29120386/995714), [Is there a GCC keyword to allow structure-reordering?](https://stackoverflow.com/q/14671253/995714) – phuclv Dec 16 '21 at 03:44
  • @EugeneSh. but there is, although it applies to the whole program instead of a few structs, so it can't be used if there are 3rd party libraries. It's useful for internal structs though – phuclv Dec 16 '21 at 12:31
  • @phuclv Thanks. Now I have another phobia :/ – Eugene Sh. Dec 16 '21 at 14:05

0 Answers0