Why Raw C Structs Make Fragile Wire Formats: Explicit Encoding Beyond Padding and Endianness
Inspect C structure layout and host byte order, then build an explicit five-byte encoder with round-trip, invalid length/version, and UBSan checks.
Writing a C structure directly with fwrite(&record, sizeof record, 1, fp), or copying it to a socket, makes an implementation's memory layout your external format instead of the fields' logical values. This article inspects structure size and integer byte order under C11 and GCC, then explicitly encodes a one-byte version and four-byte count to remove padding and host byte-order dependencies.
A wide tray with empty gaps beside a separate rail of closely arranged tiles. A conceptual illustration of structure memory layout versus explicit byte encoding.
Written and sources checked: 2026-11-28. Tested environment: Linux x86_64, GCC 14.2.0. The program builds as C11 and uses GCC's packed extension only for layout comparison. It was not run on ARM64, Windows, or other ABIs.
Field sizes do not add up to structure size
With uint8_t version and uint32_t count, the field sizes sum to five in this example's eight-bit-byte environment. Yet the actual run reported an ordinary structure size of eight and a count offset of four. A receiver expecting the count to start at the second byte would read the wrong position.
The C11 committee draft N1570 §6.7.2.1 permits internal and trailing structure padding. Section 6.2.6.1 describes cases where padding bytes have unspecified values. That is a reason to avoid making the whole memory dump a data contract. This article does not demonstrate secret leakage through padding and does not print padding bytes.
packed does not define byte order
The GCC packed documentation describes an attribute that places members to minimize required space. In the run below, the size dropped to five and the count offset to one. This alone does not create an encoder that changes integer byte order.
The example's 0x01020304 appeared in this host's memory as 04 03 02 01. Our external format instead stores the most significant byte first. Rather than adding packed to the structure, we write the format byte by byte.
| Offset | Length | External-format meaning |
| 0 | 1 byte | Version, always 01 |
| 1–4 | 4 bytes | Unsigned 32-bit count, big-endian |
| Entire record | Exactly 5 bytes | Reject extra bytes and unknown versions |
This table defines an illustrative contract for this article, not an existing standard protocol. The expected byte sequence for count 0x01020304 is 01 01 02 03 04.
Encode fields instead of copying the structure
Save the following as record_wire_demo.c.
encode() fills each byte explicitly. decode() combines values only after checking length and version, and writes the output variable only on success. Casting to uint32_t before shifting avoids assembling high bits through signed int operations. The byte buffer is not cast to a structure pointer, so this decoding path does not need to match structure offsets.
The caller must give encode() at least five writable bytes. For decode(), it must provide an input actually readable for len bytes and a valid output pointer. These functions do not discover pointer validity or allocated memory sizes. The example requires CHAR_BIT == 8 and targets implementations providing the fixed-width integer types used here.
Build and actual results
Actual output:
The checks verified round trips for zero, 0x01020304, and UINT32_MAX, plus equality with the expected byte sequence. Lengths zero, four, and six, and a length-five record with an invalid version, were rejected. The output value remained 99 after rejection. The ordinary structure size of eight and native integer bytes are observations from this environment, not assertions about other ABIs.
The program was also built and run with the following options. The same checks passed without UBSan diagnostics.
These results check the paths executed. They do not replace execution on other architectures, fuzzing the entire input space, or performance measurements.
What the transport and storage layers still need
An in-system memory layout and an external format interpreted by another program serve different purposes. Do not arbitrarily replace a library ABI or hardware-register structure with this example format. Before changing a file format, define compatibility with existing data and a version transition policy.
This code only converts one five-byte record in memory. It does not implement file-write failures, short reads, TCP framing, integrity checks, or authentication. If the count controls work or allocation sizes, add an appropriate service limit. Pointers, variable-length strings, floating-point values, and bit-fields are outside this example's encoding scope. An explicit byte format is a starting point; received lengths and business semantics still require separate validation.