Skip to main content

Practice ยท 3 of 3

Compile-Time Layout Contract

CHALLENGE
Difficulty: advanced+25 XP

ISO C11. Given struct packet { unsigned char tag; unsigned short len; unsigned int crc; }; (declared for you), implement size_t packet_min_size(void) returning sizeof(struct packet) โ€” it must be at least 7 (tag + len + crc with no padding can compress to 7 on some ABIs, but padding may grow it). Then int packet_layout_sane(void) returns 1 when offsetof(packet, crc) >= offsetof(packet, len) + sizeof(unsigned short) and 0 otherwise โ€” the members must not overlap.

Back to lesson: Practice: Portability Probes