C Reference ยท Types and Literals
Flexible array members in C
Reference for C99 flexible array members: declaring a trailing array[], allocating with malloc and offsetof, copying rules, and the old [1] hack.
A flexible array member is an array with no size declared as the last member of a struct: int items[];. It lets a single allocation hold a header and a variable amount of data right behind it. This is the standard way to write a counted buffer or a string with a stored length in one block of memory.
The struct itself has a fixed size that does not include the flexible part. You decide how many elements you need when you allocate, so you cannot create such a struct on the stack with real elements; you allocate it with malloc.
struct list { size_t count; int items[]; }; struct list *l = malloc(sizeof *l + 5 * sizeof l->items[0]);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
struct name { /* at least one other named member */ type member; type array[]; /* must be last, incomplete array type */ }; size_t bytes = sizeof(struct name) + n * sizeof(type); /* or, avoiding trailing padding waste: */ size_t bytes2 = offsetof(struct name, array) + n * sizeof(type);
| Part | Description |
|---|---|
array[] |
Declares the flexible member. Only allowed as the last member and only in a struct with at least one other member. |
sizeof(struct name) |
Size of the struct without the array (possibly including padding before it). |
offsetof(struct name, array) |
Where the array starts; may be smaller than sizeof, so it can save a few bytes. |
Errors / undefined behavior: accessing an element beyond what you allocated is undefined behavior. A struct with a flexible array member cannot be a member of another struct or an element of an array (GCC allows it as an extension in some cases). Assigning one such struct to another copies only the fixed part.
Usage notes
- Allocate on the heap:
malloc,callocorrealloc. Check fornullptrand for overflow inn * sizeof(type)(seeckd_mul). - Copy by hand.
*a = *bcopies only the fixed members. Usememcpywith the full size. - Do not put
sizeofon the member.sizeof l->itemsis a constraint violation because the type is incomplete; keep the element count in another member. - Resizing:
reallocthe whole block, then update the count. - Pre-C99 hack:
int items[1];oritems[0]worked in practice but is undefined behavior when indexed beyond the declared length. Modern compilers (with-fstrict-flex-arrays) treat them as real bounds. - Compare with a pointer member:
int *itemsneeds a second allocation and a secondfree, but allows separate resizing and copying by assignment. - C23 keeps the feature unchanged; the
counted_byattribute is a compiler extension for bounds checking, not standard C.
Related
struct,malloc,realloc, Struct padding,stddef.h- Lessons: Dynamic memory, Structures