In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is C language flexible array, I believe many inexperienced people are helpless about this, this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.
preface
Maybe everyone will be a little confused when they first see this title. What is a flexible array? How come I have never heard of it? However, flexible arrays do exist, and they often appear in some company interview questions. Today, follow the author to learn flexible arrays.
Tip: The following is the text of this article, the following cases for reference
What is a flexible array?
In C99, the last element in the struct is allowed to be an array of unknown size, which is called a flexible array, for example:
struct st_type{ int i; int a[0];//flexible array member, can also be written int a[];};
Structure member a array, its array size is not determined, in the future if necessary can be large or small.
Some compilers support a[0], and some compilers support a[ ], depending on the compiler.
2. Characteristics of flexible arrays
1. Flexible array members must be preceded by at least one other member in a struct
Examples are as follows:
struct st_type{ int i; int a[0];//flexible array member, can also be written int a[];};
For example, in the above code, if you want to create a flexible array a, you must create some other members first.
2. sizeof The size of this structure returned does not include memory for flexible arrays
Examples are as follows:
struct st_type{ int i;//4 bytes int a[0];//flexible array member, can also write int a[]; //because it is a flexible array, cannot confirm how many bytes a}; int main() { printf("%d\n", sizeof(struct st_type));//printf 4 return 0; }
The size of the struct containing the flexible array is calculated here. Because the flexible array itself cannot be determined how many bytes there are, the calculation of the flexible array will be omitted when calculating the overall structure size.
3. Structures containing flexible array members allocate memory dynamically using the malloc() function, and the allocated memory should be larger than the size of the structure to accommodate the expected size of the flexible array.
ps: In addition to the malloc function, realloc, calloc and other dynamic memory opening functions also require similar operations.
For example, I want array a to have 10 elements in it, and now I want to malloc it.
Examples are as follows:
#include#includestruct st_type{ int i;//4 bytes int a[0];//flexible array member, can also be written int a[];};int main(){ //suppose I now need 10 elements in a struct st_type*ps=(struct st_type*)malloc(sizeof(struct st_type) + 10 * sizeof(int)); if (ps == NULL)//malloc fails to open due to insufficient space. If it fails, a null pointer will be returned. { printf("%s\n", strerror(errno)); return -1;//The program jumps out after the program has a problem } //Open successfully int j = 0; for (j = 0;j
< 10;j++) { ps->a[j] = j; } for (j = 0;j
< 10;j++) { printf("%d ", ps->a[j]);//Print 0-9 } printf("\n"); //if you want to continue printing with flexible array a //For example, now there are only 10 elements in a, I have used up 10, I still have to continue to add 10, with realloc struct st_type*ptr=realloc(ps, sizeof(struct st_type) + 20 * sizeof(int));//ps:realloc The second parameter is the adjusted overall size if (ptr == NULL) { printf("expansion failed\n"); return -1; } else { ps = ptr; } //expansion successful int k = 0; for (k = 10;k
< 20;k++) { ps->a[k] = k; } for (j = 0;j
< 20;j++) { printf("%d ", ps->a[j]);//Print 0-19 } //Free up space free(ps); ps = NULL; return 0;}
Here we need array a has 10 elements, so we malloc time to open up 4 bytes for the integer i in the struct, and then open up 40 bytes for the integer array a, and then malloc function returns the start address of the open space, assigned to the ps pointer of type truct st_type *.
malloc(sizeof(struct st_type) + 10 * sizeof(int)) This operation is equivalent to struct st_type creating the space occupied by a variable, but using malloc to open up
When you change the size of array a and add space, realloc(ps, sizeof(struct st_type) + 20 * sizeof(int)), the first parameter of realloc is still ps, because you are using malloc to open up a piece of space at a time, you cannot adjust the space of array a alone.
The advantages of flexible arrays
Flexible array is to realize dynamic opening of a piece of space, then we have also talked about pointers to dynamic memory opening before, let's look at a piece of code to compare these two methods:
//You can also use the pointer to dynamically change the space pointed to by struct st_type{ int i;//4 bytes int *a;//4 bytes, where the calculation structure size is exactly 8 bytes};int main(){ struct st_type*ps = (struct st_type*)malloc(sizeof(struct st_type)); ps->i = 100; ps->a = (int*)malloc(10 * sizeof(int));//a points to 40 bytes of space managed by int* int j = 0; for (j = 0;j
< 10;j++) { ps->a[j] = j;//a[j]=*(a+j) } for (j = 0;j
< 10;j++) { printf("%d", ps->a[j]); } //a There is not enough space to point to. I want to resize it. int *ptr = (int*)realloc(ps->a, 20 * sizeof(int)); if (ptr == NULL) { printf("expansion failed"); return -1; } else { ps->a = ptr; } //Use... //Release free(ps->a); ps->a = NULL; free(ps); ps = NULL;}
Note here that to free space, you have to first free the space pointed to by pointer a, and then release the structure pointer.
As shown in the above figure, our structure pointer ps opens up a space in which to store the integer i and the integer pointer a, a and malloc (if necessary, realloc can be added later). If you release ps first, a will be gone, and you will not be able to find the space pointed to by a.
Or that vivid example, for example, a is a police chief, the space opened by malloc is an undercover, only a knows that undercover, now that the police chief is dead, you can no longer prove that the undercover is an undercover, that is to say, after a disappears, there is no way to release the space opened, which will cause memory leakage. To realize dynamic memory adjustment of pointers, it is necessary to explain a certain sequence of pointer release.
Here, in contrast to flexible arrays, flexible arrays and pointers above can be adjusted for the size of a piece of space, but flexible arrays have two advantages:
The first benefit: easy memory release
If our code is in a function for someone else, you make a secondary memory allocation in it and return the entire structure to the user. The user calls free to release the struct, but the user doesn't know that members of the struct need free, so you can't expect the user to discover it. Above, if I allocate the memory of the structure and the memory that its members want at one time, and return a pointer to the structure to the user, the user can free all the memory once, and do not consider the order of release mentioned above.
The second benefit: faster access
Contiguous memory is good for faster access and less memory fragmentation.
ps: Memory fragmentation as shown below
The operating system gives us a piece of memory. When we do malloc, it doesn't have to be a piece of memory. The blank part in the above picture is memory fragments, some of which are similar to our cutting cloth and some leftover scraps.
After reading the above, do you know what is the method of flexible array in C language? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.