Flexible Array Members
A flexible array member is an incomplete array as the last member of a struct. You over-allocate the struct so the extra bytes become that array, keeping the header and the payload in one allocation.
Detailed Explanation
C99 added this syntax:
struct Packet {
size_t len;
unsigned char data[]; /* flexible array member */
};
data has no compile-time length. sizeof(struct Packet) is the size of the header, including any padding before data, and does not count any payload. You allocate sizeof(struct Packet) + n bytes and then treat data as an array of n elements. One malloc, one free, and the payload sits immediately after the header, which is kind to the cache.
The member must be last. The struct must contain at least one named member before it. You cannot create an automatic object and expect data to have storage; struct Packet p; has no payload. You cannot put the struct in an array in a way that uses sizeof to stride to the next header — the payload size varies. You cannot embed the struct as a non-last member of another struct.
The old GCC extension was data[0], the "zero-length array." The C99 spelling is data[]. A pre-C99 hack used data[1] and allocated sizeof(struct Packet) + (n - 1), which is easy to get wrong and which overstates sizeof. Prefer the standard member.
Alignment still applies. If data is double[] and the header ends on a 4-byte boundary, the compiler inserts padding so data is correctly aligned. sizeof(struct Packet) includes that padding. Allocate sizeof(struct Packet) + n * sizeof *p->data, not a hand-counted header size.
Copying is a trap. Assigning *a = *b copies the header only. The payload stays behind. Use memcpy of the full allocated size, or copy the header and then the len bytes of data.
Code Examples
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Packet {
size_t len;
unsigned char data[];
};
static struct Packet *packet_new(const unsigned char *src, size_t n)
{
struct Packet *p = malloc(sizeof *p + n);
if (p == NULL) {
return NULL;
}
p->len = n;
memcpy(p->data, src, n);
return p;
}
int main(void)
{
unsigned char raw[] = { 'h', 'i' };
struct Packet *p = packet_new(raw, sizeof raw);
if (p == NULL) {
return 1;
}
printf("%zu %c%c\n", p->len, p->data[0], p->data[1]); /* prints: 2 hi */
free(p); /* one free */
return 0;
}#include <stdio.h>
#include <stddef.h>
struct Packet {
size_t len;
unsigned char data[];
};
int main(void)
{
printf("%zu %zu\n", sizeof(struct Packet), offsetof(struct Packet, data));
/* sizeof is the header, equal to offsetof(data) on typical ABIs */
return 0;
}Common Gotchas
`sizeof(struct Packet)` does not include the payload
allocating only `sizeof *p` gives you a header and a `data` that you must not index.
Struct assignment copies the header, not `data`
after `*a = *b`, `a->data` still names `a`'s extra bytes, now holding `b`'s `len` and the wrong contents.
A flexible array member cannot be the only member, and it cannot be followed by another member
both are constraint violations.
Do not make an array of these structs
the stride would be the header size and would overlap payloads.