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/01 Report--
This article mainly introduces how to use flexible array in C language, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
one。 Characteristics of flexible arrays struct S {int x; int a [];}; int main () {printf ("% d", sizeof (S));}
What is the output of this code?
How many bytes does this a [] take up when we print the size of the space occupied by structure S?
The output is 4, but an x of type int is 4. Where is a []? That's weird.
It turns out that this is a flexible array.
1. The last element in the structure is allowed to be an array of unknown size, which is a flexible array.
two。 The flexible array in the structure must be preceded by at least one other member.
The size of this structure returned by 3.sizeof does not include the memory of flexible arrays.
4. Structures containing flexible array members use the malloc function to dynamically allocate memory, and the allocated memory should be larger than the size of the structure to accommodate the expected size of the flexible array.
two。 The use of flexible arrays. How to use flexible arrays
Structures containing flexible array members use malloc functions to dynamically allocate memory
And the allocated memory should be larger than the size of the structure to accommodate the expected size of the flexible array.
# define _ CRT_SECURE_NO_WARNINGS 1#include#include struct S {int x; int a [];}; int main () {/ / opens up 40 bytes of space for flexible array a [] struct S* ps = (struct S*) malloc (sizeof (struct S) + 40) If (ps = = NULL) / / check if the null pointer {perror ("ps"); return 1;} ps- > x = 10 Int I = 0; for (I = 0; I
< 10; i++) { ps->A [I] = I; / the array uses} for (I = 0; I)
< 10; i++) { printf("%d ",ps->A [I]); / / Array print} / / if 40 is not enough, realloc can be used to expand the capacity / / for example: struct S * ptr = (struct S *) realloc (ps, sizeof (struct S) + 80) If (ptr = = NULL) / / check if the null pointer {perror ("realloc"); return 1;} else {ps = ptr;} free (ps) / / Free memory and set null pointer ps = NULL;} 2. What's the substitute if you don't need a flexible array?
We often use string pointers to request space.
Then let's just give the string pointer malloc a piece of space, won't we?
Why use flexible arrays?
# define _ CRT_SECURE_NO_WARNINGS 1#include#include struct S {int x; int* a;}; int main () {struct S* ps = (struct S*) malloc (sizeof (struct S)); / / Open up space if for structural variable x (ps = = NULL) {return 1;} ps- > a = (int*) malloc (40) / / Open up 40 bytes of space for string pointers if (ps- > a = = NULL) {free (ps); ps = NULL; return 1;} free (ps- > a); ps- > a = NULL; free (ps) Ps = NULL;}
The above code can indeed accomplish the same function.
But flexible arrays are relatively better.
Let's look at the advantages of flexible arrays.
three。 The advantage of flexible array 1. Facilitate memory release
With flexible arrays, we only use free once.
Using string pointers requires free twice, so there is a risk of memory leakage.
two。 Improve access speed
Flexible array is malloc once, which is continuous memory.
It is beneficial to improve access speed and reduce memory fragmentation.
Thank you for reading this article carefully. I hope the article "how to use flexible Array in C language" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.