In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the methods of using arrays". The content of the explanation is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought. Let's study and learn "what are the methods of using arrays"?
Let's take a look at the following code, how much space is occupied by the two structures Test0 and Test1, which contain dynamic string members, respectively?
/ / Source: official account [programming Zhuji] / / author: Mr. include struct Test0 # include struct Test0 {int a; int b; char * c;}; struct Test1 {int a; int b; char c [];}; int main (void) {printf ("sizeof (struct Test0) =% zd\ n", sizeof (struct Test0)) Printf ("sizeof (struct Test1) =% zd\ n", sizeof (struct Test1)); return 0;}
Many readers can see at a glance that on a 64-bit system, when compiled into a 64-bit program, the output is:
16 8
There is usually no doubt that the result of Test0 is 16, after all, 4 (int) + 4 (int) + 8 (pointer) = 16, but some readers may have questions about the structure of the latter taking up 8 bytes of space. (for byte alignment, refer to "Byte alignment, see this article.")
Flexible array (flexible array)
In fact, this is a feature of flexible arrays introduced in C99. That is, the last member of a structure can be an array of incomplete types (an array that lacks sufficient information to describe a complete object), but it makes the size of the entire structure as if it did not have such a member. However, when you use a structure to access this member by this name, it is like visiting a normal array member.
If the array ends up with no elements, accessing the array will be undefined behavior.
As we have seen before:
Struct Test1 {int a; int b; char c [];}
The member c is an array, but does not specify a size, uses sizeof to calculate Test1, and its footprint is only 8 bytes.
What's the advantage?
So what are the benefits of using flexible arrays?
Memory request and release
Suppose you use two types of structures to store 16 bytes of character data, and you need to request memory. For struct Test0:
Strcut Test0 * t0 = malloc (sizeof (struct Test0)); / / request memory for the structure t0-> c = malloc (sizeof (char) * 16); / / request memory for the data pointed to by the member
For struct Test1:
Strcut Test1 * T1 = malloc (sizeof (struct Test1) + sizeof (char) * 16)
Do you see the difference? The former requires two memory requests, while the latter only needs one. The former address is not contiguous (twice malloc), while the latter address is contiguous. When you visit member c, you only need to do the following:
T1-> c, just like an ordinary member.
It is also easy to determine whether their addresses are contiguous, simply by printing the addresses of b and c, respectively.
Similar to memory release, the memory requested by member c needs to be freed separately, while the latter can be freed together.
Data copy
It is precisely because of the previous difference that there is a greater difference when copying data.
For struct Test0:
/ / memcpy (t0copyrecovert0precinct sizeof (struct Test0)); / / No, so the c of t0copy and the c of T0 point to the same area of memory. T0t0copy.a = t0.a; t0t0copy.b = t0.b; memcpy (t0copy.cdirection t0.cauthoring sizeof (char) * 16)
You can't copy it all at once, because its member c is a pointer type, and what we need is a full copy, so we have to copy the memory it points to. Is the structure member assignment a deep copy or a shallow copy? ")
But for struct Test1:
Memcpy (t0copyreparing t0pencil sizeof (strcut Test1) + sizeof (char) * 16)
Here, because of the memory of the flexible array, its data content and the address of the structure data member are contiguous, so it can be copied directly.
Reduce memory fragmentation
Because the flexible array of structures and the addresses of structure members are contiguous, memory can be applied for together, thus memory fragmentation is avoided to a greater extent. In addition, because the member itself does not take up space in the structure, on the whole, it takes up slightly less space than ordinary array members.
Zero-length array
Similar to the flexible array function, there is a 0-long array, but it is not standard, but it can achieve similar functions, using the following ways:
Struct Test1 {int a; int b; char c [0];}
The difference is that the length of the array is 0. However, since it is not part of the C standard, it is not recommended for portability unless you cannot use C99.
Thank you for your reading. The above is the content of "what is the use of arrays?" after the study of this article, I believe you have a deeper understanding of the use of arrays. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.