Zero length arrays don t make sense? That s what you don t understand! See how to play it in the Lin

Mondo Education Updated on 2024-02-23

C zero-length arrays, which may sound a bit strange because it doesn't allocate memory space to store data. But in reality, zero-length arrays are everywhere in the Linux kernel.

First, we need to understand what a zero-length array is. In simple terms, a zero-length array is an array of length 0, i.e. an array that does not contain any elements. Zero-length arrays were introduced in the C99 standard and further supported in C11. The definition is simple, it's an array of size 0. For example:

int a[0];
In the Linux kernel, zero-length arrays are not usually used as such, but as the last element in the struct, in conjunction with dynamic memory allocation.

In the Linux kernel, zero-length arrays can often be seen being used as placeholders at the end of structs to represent variable-length portions of structs. For example, a struct sockaddr struct representing a network socket might look like this:

In this example, the sa data field is actually a populated field that holds address data from different address families. Since address families can be different and the required data length can be different, a sufficiently large fixed-length array is used here. However, if you use a zero-length array, ** will be clearer:

In practice, the kernel will set the required SA data length in conjunction with dynamic memory allocation and populate the relevant data. Zero-length arrays can be combined with memory allocation functions such as kmalloc and vmalloc to achieve this dynamic allocation, so some people also call zero-length arrays flexible arrays.

In the Linux kernel or other low-level systems written in C, zero-length arrays are often used as part of flexible data structures, especially in arrays that need to grow or shrink dynamically. Here's a simple example of how to implement a variable-length integer array using a zero-length array in kernel programming

In this example, ignore the kernel module and focus on the variable int array functions.

We define a struct called variable int array, which contains a length field and a zero-length array data. Use the create int array function to allocate memory and initialize the struct, and the destroy int array function to free memory. The Add int to array function allows us to add new integers to the array, and it dynamically reallocates memory to accommodate the newly added elements. Finally, the print int array function is used to print out the value of the integer dynamic array member in the struct.

Let's take a look at the implementation of key **.

The create int array function creates a new variable int array structure of a variable-length integer array, and the function parameter initial length is the initial length of the array. Line 13 uses kmalloc to dynamically allocate the initial memory space of the struct, which includes the struct itself and the space of an integer array of initial length of initial length. Line 24 is to store the initial length, which is the initial data length value, in the length member of the struct, because the length is not 0 but the initial length.

Destroy int array is to call kfree to release the memory space created above, which is relatively simple.

Focus on the Add int to Array(struct variable int array **array ptr, int value) function, which is the most troublesome process to dynamically add a new integer value to the array.

The first parameter is the struct array ptr, which is a second-level pointer to the first address of the old struct memory, note that the new allocated memory space address is stored in this pointer variable. The second parameter value is a new integer value that has been added.

Line 43 is to store the old struct header address in the array pointer.

Line 44 new length temporarily holds the length of the array.

Line 47 allocates new memory space and stores the first address in the array variable, noting that from now on the array points to the new space. Because a new integer has been added to the array, the space becomes larger and needs to be redistributed, and the newly allocated space size includes the previous struct length and the space size of the newly added integer.

Line 59 is to copy the old array data into the new array space.

Line 62 is the last position where the new integer value is added to the new array space, and the data of the array space is updated.

Line 66 updates the length member of the struct to new length, which is actually a 1.

Line 69, all memory of the old structure is freed, as new memory is allocated as the length increases.

Line 72 is to assign the new space address to the array ptr pointer variable, which is to make the pointer to the old struct header address point to the new struct header address, and that's it.

To put it simply, a zero-length array is an array of 0 length. But in programming, it's often used as a placeholder, or as the last element of a struct, which gives the flexibility to store more data in the struct.

So, what is the value and significance of zero-length arrays?

Flexibility: Zero-length arrays allow us to allocate a basic struct without knowing exactly how much storage space we need. In this way, we can dynamically add data to this zero-length array as needed in subsequent program executions. This flexibility is useful for working with variable-sized data.

Memory efficiency: By dynamically allocating memory to zero-length arrays, we can avoid allocating too much memory in the first place, which allows for more efficient use of memory resources. The extra memory is allocated only if we really need it.

Simplify**: In some cases, the structure can be simplified by using zero-length arrays. For example, we can put some related data in a struct, and a zero-length array can be used as the last element of this struct to store additional data. In this way, we can manage and manipulate this data more easily.

The follow-up series of high-quality articles will continue to be updated, and it is not easy to code words, welcome to follow, like, collect and ask questions.

Related Pages