When to Use Flexible Arrays vs Pointer Arrays in C Embedded Programming
This article explains the concept of flexible arrays in C, compares them with pointer‑based arrays, shows allocation and deallocation code, and discusses memory continuity, performance, fragmentation risk, and suitable usage scenarios for embedded systems.
What is a Flexible Array?
A flexible array is defined by placing an array member of length 0 or 1 as the last field of a struct, allowing the struct to hold variable‑length data directly after the fixed fields.
typedef struct {
uint16_t length;
uint8_t data[1]; // flexible array
// other members
} FlexArrayStruct;When using it, allocate enough memory for the struct plus the actual data size:
FlexArrayStruct* flex = malloc(sizeof(FlexArrayStruct) + 99);
flex->length = 100; // actual data lengthPointer‑Based Array Alternative
The pointer approach stores a separate pointer to dynamically allocated data.
typedef struct {
uint16_t length;
uint8_t* data; // pointer
// other members
} PointerStruct;Allocation requires two steps:
PointerStruct* ptr = malloc(sizeof(PointerStruct));
ptr->data = malloc(100);
ptr->length = 100;Key Differences
Memory continuity : The flexible array keeps struct fields and data in one contiguous block, while the pointer version stores them in separate blocks and adds an extra pointer field (4‑8 bytes).
Allocation and release : Flexible arrays need a single malloc and a single free. Pointer arrays need two malloc calls and two corresponding free calls, with the data block freed before the struct.
// Flexible array
FlexArrayStruct* flex = malloc(sizeof(FlexArrayStruct) + (length - 1));
free(flex);
// Pointer array
PointerStruct* ptr = malloc(sizeof(PointerStruct));
ptr->data = malloc(length);
free(ptr->data);
free(ptr);Access efficiency : Accessing a flexible array is a simple offset from the struct address, avoiding an extra dereference. Pointer arrays require an additional pointer lookup, which can add a memory jump and affect high‑frequency processing.
Fragmentation risk : A single contiguous allocation for flexible arrays reduces fragmentation, making them suitable for memory‑constrained embedded environments. Pointer allocations may create multiple small blocks, increasing the chance of fragmentation and potential leaks over long runtimes.
When to Use Which
Use flexible arrays when you need contiguous memory, simple allocation/deallocation, and high access speed—common in BLE stacks, sensor data handling, and other high‑frequency embedded scenarios. Choose pointer arrays when the same data must be shared among multiple structs or when the data length changes frequently and sharing is required.
Conclusion
Both techniques have merits, but in most embedded cases the flexible array wins because of its memory continuity, straightforward management, and better performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
