Why Zero‑Length Arrays Matter in Linux Kernel Development
This article explains what zero‑length arrays are, how they are defined in C, why they appear frequently in the Linux kernel as flexible array members, and provides a complete kernel‑style implementation showing creation, expansion, and cleanup of a dynamically sized integer array.
Zero‑Length Arrays in C
A zero‑length array is declared with a size of 0. It was introduced as a GNU extension and later accepted by the C99 and C11 standards as a way to create a placeholder for a variable‑size region at the end of a structure. int a[0]; Although the array contains no elements, the compiler allows the structure to be followed in memory by additional data that can be allocated dynamically.
Typical Kernel Usage
In the Linux kernel a zero‑length (or flexible) array is placed as the last member of a struct. The struct header is allocated together with the required payload size, enabling a single contiguous memory block.
struct sockaddr {
sa_family_t sa_family; // address family, e.g. AF_INET
char sa_data[0]; // variable‑size payload
};When the structure is allocated, the caller decides how many bytes to reserve for sa_data and copies the actual address data into that region.
Kernel‑Style Dynamic Integer Array Example
The following code demonstrates a simple dynamically‑sized integer array using a zero‑length member. It is written for kernel space and uses kmalloc / kfree for memory management.
#include <linux/kernel.h>
#include <linux/slab.h>
/* Structure with a zero‑length array */
struct variable_int_array {
size_t length; // number of valid elements
int data[0]; // placeholder; actual storage follows the struct
};
/* Allocate a new array with the requested initial length */
static struct variable_int_array *create_int_array(size_t initial_length)
{
struct variable_int_array *arr;
arr = kmalloc(sizeof(*arr) + initial_length * sizeof(int), GFP_KERNEL);
if (!arr)
return NULL;
arr->length = initial_length;
return arr;
}
/* Release the memory */
static void destroy_int_array(struct variable_int_array *arr)
{
if (arr)
kfree(arr);
}
/* Append a value, reallocating the block as needed */
static int add_int_to_array(struct variable_int_array **p_arr, int value)
{
struct variable_int_array *old = *p_arr;
size_t new_len = old->length + 1;
struct variable_int_array *new_arr;
new_arr = kmalloc(sizeof(*new_arr) + new_len * sizeof(int), GFP_KERNEL);
if (!new_arr)
return -ENOMEM;
/* Copy existing elements */
memcpy(new_arr->data, old->data, old->length * sizeof(int));
new_arr->data[new_len - 1] = value; // store the new element
new_arr->length = new_len;
kfree(old);
*p_arr = new_arr;
return 0;
}
/* Print the contents (kernel log) */
static void print_int_array(const struct variable_int_array *arr)
{
size_t i;
for (i = 0; i < arr->length; ++i)
printk(KERN_INFO "%d ", arr->data[i]);
printk(KERN_INFO "
");
}Explanation of Key Functions
create_int_array : Calls kmalloc for a block that contains the struct header plus initial_length integers. The length field is initialized accordingly.
add_int_to_array : Computes the new size, allocates a larger block, copies the existing data with memcpy, stores the new integer, updates length, frees the old block, and updates the caller’s pointer.
destroy_int_array : Frees the allocated block with kfree.
print_int_array : Iterates over data and prints each element using printk.
Advantages of Zero‑Length (Flexible) Arrays
Flexibility : The structure can accommodate an arbitrary amount of data without defining a fixed maximum.
Memory efficiency : Only the exact number of elements needed is allocated, avoiding wasted space.
Simplified code : Related metadata and payload reside in a single contiguous allocation, reducing pointer arithmetic and the need for separate buffers.
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.
