I am looking for a way to determine the size of an unnamed structure because I want to explicitly calculate the padding.
I have a set of structures that share a common layout
struct Packet { uint8_t AllocatorDebugInfo[ALLOC_SIZE ]; uint8_t Padding[ ]; uint8_t Header[HEADER_SIZE ]; uint8_t Payload[PAYLOAD_SIZE ]; };
I have the following requirements:
- I wrote a special dispenser that has debug information before the package.
- I want plug-in header types, while preserving the payload in all types of package layouts.
I am trying to calculate the size of the header as shown in the code below:
typedef union { struct { } SmallHeader; struct { } MedHeader; struct { } LargeHeader; } HeaderSizes; HeaderSizes *p; enum { SMALL_PADDING = sizeof(HeaderSizes) - sizeof(p->SmallHeader)) };
Unfortunately, the memory is tight, and I am looking for ways to avoid the global pointer.
EDIT:
Trying to figure out an additional complement like this is apparently a very bad idea. This only works as long as you are aware of the fact that the largest structure will have more indentation than expected (zero).
enum { LARGE_PADDING = sizeof (HeaderSizes) - sizeof ((HeaderSizes*)0->LargeHeader) }; struct LargePacket { uint8_t AllocDebug[ALLOC_SIZE]; uint8_t Padding[ LARGE_PADDING ]; uint8_t Payload[ PAYLOAD_SIZE ]; };
source share