How does one determine the size of an unnamed struct?

Viewed 1923

I'm looking for a way to determine the size of an unnamed struct because I want to explicitly calculate padding.

I have set of structures that have a common a general layout

struct Packet {
  uint8_t AllocatorDebugInfo[ALLOC_SIZE /* constant across all structs */ ];
  /* OPTIONAL */ uint8_t Padding[       /* Needs to be determined */      ];
  uint8_t Header[HEADER_SIZE            /* dependent on  packet type */   ];
  uint8_t Payload[PAYLOAD_SIZE          /* constant across all structs*/  ];
}; 

I have the following requirements:

  • I have written a special purpose allocator that has debugging information in front of the packet.
  • I want exchangeable header types while keeping the payload across all packet types layout compatible.

I'm trying to calculate the Header Size as shown in the code below:

typedef union {
  struct { /* opaque */ } SmallHeader;
  struct { /* opaque */ } MedHeader;
  struct { /* opaque */ } LargeHeader;
} HeaderSizes;

HeaderSizes *p;
enum { SMALL_PADDING = sizeof(HeaderSizes) - sizeof(p->SmallHeader)) };

Unfortunately memory is tight and I'm looking ways to avoid the global pointer.

EDIT:

Apparently trying to calculate an optional padding like this is a very bad idea. This only works as long as you are aware of the fact, that the biggest struct will have more padding than expected (zero).

enum { LARGE_PADDING = sizeof (HeaderSizes) - sizeof ((HeaderSizes*)0->LargeHeader) };
struct LargePacket {
  uint8_t AllocDebug[ALLOC_SIZE];
  uint8_t Padding[ LARGE_PADDING ]; /* will evaluate to 0 -- struct size 1 */
  uint8_t Payload[ PAYLOAD_SIZE ];  /* off by 1 */
};
1 Answers
Related