Flexible Arrays vs Pointers in C Language

Click the blue “One Bite Linux” in the upper left corner, and select “Set as Favorite

Get the latest technical articles first

☞【Technical Content】Learning Path for Embedded Driver Engineers
☞【Technical Content】Linux Embedded Knowledge Points - Mind Map - Free Access
☞【Employment】A Comprehensive Project Based on Linux IoT for Your Resume
☞【Employment】Resume Template





Flexible Arrays vs Pointers in C LanguageIntroducing a knowledge point learned in recent C language programming: flexible arrays

In C language, when handling variable-length data, flexible arrays and pointers are two common solutions. Especially in embedded development, where memory resources are limited and efficiency is crucial, choosing the right solution is essential.

  • Flexible Arrays

A flexible array is defined as the last member of a structure being an array of length 0 or 1, used to store variable-length data.

typedef struct{  uint16_t  length;  uint8_t  data[1]; // Flexible array // Define other attributes} FlexArrayStruct;

When used, the actual data length is determined through dynamic memory allocation:

FlexArrayStruct* flex = malloc(sizeof(FlexArrayStruct) + 99);flex->length = 100; // Pointer array typedef struct{  uint16_t length;  uint8_t* data; // Pointer  // Define other attributes} PointerStruct;

Using pointers requires two memory allocations:

// First allocate the structure PointerStruct* ptr = malloc(sizeof(PointerStruct));// Then allocate data memory ptr->data = malloc(100);ptr->length = 100;

Differences Between Flexible Arrays and Pointer Arrays

Memory Contiguity:

In flexible arrays, the structure members and data are stored in a contiguous memory block, while in pointers, the structure and data are stored in two separate memory areas, requiring additional storage for a pointer (4-8 bytes).

Allocation and Deallocation:

Flexible data requires only one malloc allocation and one free deallocation, making it simple to operate.


// Allocate FlexArrayStruct* flex = malloc(sizeof(FlexArrayStruct) + (length - 1));// Free free(flex);

The pointer solution requires two mallocs and two frees.

// Allocate PointerStruct* ptr = malloc(sizeof(PointerStruct));ptr->data = malloc(length);// Free free(ptr->data); // Must free data first free(ptr); // Then free the structure

Access Efficiency

Flexible arrays access data directly through the structure address without additional pointer dereferencing, resulting in faster access speed. The pointer solution requires accessing the pointer first, then accessing the data through the pointer, adding an extra memory jump.

In high-frequency processing scenarios, there may be some performance differences.

Memory Fragmentation Risk

Flexible arrays allocate contiguous memory at once, reducing the risk of memory fragmentation, making them suitable for embedded systems and other environments with limited memory resources. The pointer solution, with two allocations, may create more small memory fragments, potentially leading to “memory leaks” or “inability to allocate large contiguous memory” in long-term operation.

Usage Scenarios

When data needs to be shared among multiple structures: avoid data copying by sharing the same data through pointers. For frequently changing data lengths, the pointer solution is preferred; otherwise, flexible arrays should be prioritized.

Conclusion

Both flexible arrays and pointers have their advantages and disadvantages, but in most embedded scenarios, flexible arrays are a better choice due to their good memory contiguity, simple allocation and deallocation, and high access efficiency.

  • Especially in high-frequency data interaction scenarios such as BLE protocol stacks and sensor data processing, flexible arrays can significantly enhance system stability and efficiency.

end

One Bite Linux

Follow and reply with 【1024】 to receive a wealth of Linux materials

Collection of Exciting Articles

Article Recommendations

【Collection】ARM【Collection】Fan Q&A【Collection】All OriginalsCollectionLinuxIntroductionCollectionComputer NetworksCollectionLinux Drivers【Technical Content】Learning Path for Embedded Driver Engineers【Technical Content】All Knowledge Points of Linux Embedded – Mind Map

Leave a Comment