1. Preface
Today in the code to see a structure containing char data[0], the first time I see it is strange, the length of the array can be zero. So the internet search for the purpose of such usage, found in the Linux kernel, the structure is often used in the data[0]. The purpose of this design is to make the length of the array variable and to allocate as needed. Convenient operation, save space.
2, DATA[0] Structure
The structural shapes that are often encountered are as follows:
struct buffer
{
int data_len; Length
char data[0]; Start Address
};
In this structure, data is an array name, but the array has no elements; the real address of the array follows the structure buffer, which is the address of the data after the structure body (if the content allocated to the struct is larger than the actual size of the structure), The extra part of the following is the content of this data, which can be cleverly implemented in the C language of the array extension.
Write a program that compares Char Data[0],char *data, Char data[], as follows:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <stdint.h> 5 6
typedef struct 7 {8 int data_len;
9 Char data[0];
Ten}buff_st_1;
one typedef struct {int data_len; char *data}buff_st_2;
The typedef struct {int Data_len char data[];}buff_st_3; num int main () ("sizeof" (buff_st_1) =%u\n, sizeof (buff_st_1)), (sizeof) buff_st_2,
sizeof (buff_st_2));
printf ("sizeof (Buff_st_3) =%u\n", sizeof (Buff_st_3));
Buff_st_1 buff1;
Buff_st_2 buff2;
Buff_st_3 BUFF3; From printf ("Buff1 address:%p,buff1.data_len address:%p,buff1.data address:%p\n", &buff1, & (buff1
. Data_len), buff1.data); ("Buff2 Address:%p,buff2.data_len address:%p,buff2.data address:%p\n", &buff2, & (buff2
. Data_len), buff2.data); printf ("Buff3 Address:%p,buff3.data_len address:%p,Buff3.data address:%p\n ", &BUFF3, & (Buff3.data_len), buff3.data);
0; 44}
From the results you can see that data[0] and data[] do not occupy space, and the address is immediately behind the structure, and Char *data as a pointer, occupies 4 bytes, the address is not after the structure.
3, the actual usage
In the actual program, the length of the data is unknown, so it is convenient to save space by the variable length array. Manipulation of pointers facilitates conversion of data types. The test procedure is as follows:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #i
Nclude <stdint.h> 5 6 typedef struct 7 {8 int data_len;
9 Char data[0];
Ten}buff_st_1;
one typedef struct {int data_len; char *data}buff_st_2;
The typedef struct {int Data_len char data[];}buff_st_3;
typedef struct {uint32_t id; uint32_t age;}student_st; Print_stu void (const Student_st *stu) ("id:%u,age:%u\n", Stu->id, Stu->age); 34} 35 3 6 int main () (Student_st *stu = (Student_st *) malloc (sizeof (Student_st)); stu->id
Gt;age = 23;
Student_st *tmp = NULL;
Buff_st_1 *buff1 = (buff_st_1 *) malloc (sizeof (buff_st_1) + sizeof (Student_st));
Buff1->data_len = sizeof (STUDENT_ST);
memcpy (Buff1->data, Stu, Buff1->data_len); In printf (