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 > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is C++ flexible array". In daily operation, I believe many people have doubts about what C++ flexible array is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is C++ flexible array"! Next, please follow the editor to study!
1 introduction fixed length array package
In peacetime development, when sending and receiving buffer data, if you use a buffer packet of fixed length, it is assumed that the size is 1k and the maxim length is 1024. The structure is as follows:
/ / fixed length buffer
Struct max_buffer
{
Int len
Char data[MAX _ LENGTH]
}
Size of data structure > = sizeof (int) + sizeof (char) * MAX_LENGTH to prevent data overflow, the length of data is generally set to be large enough, but it is precisely because of this that the array is redundant.
If you send 512 bytes of data, it will waste 512 bytes of space, usually communication, most of the heartbeat packet, the size is much less than 1024, in addition to wasting space but also consumes a lot of traffic.
Memory request:
If ((m_buffer = (struct max_buffer *) malloc (sizeof (struct max_buffer))! = NULL)
{
Masked buffer-> len = CUR_LENGTH
Memcpy (masked buffer-> data, "max_buffer test", CUR_LENGTH)
Printf ("% d,% s\ n", masking buffer-> len, masking buffer-> data)
}
Memory release:
Free (m_buffer)
M_buffer = NULL; pointer packet
In order to avoid the waste of space, we can replace the above fixed-length array of MAX_LENGTH with pointers, and dynamically open up CUR_LENGTH-sized space each time we use it. Packet structure definition:
Struct point_buffer
{
Int len
Char * data
}
Data structure size > = sizeof (int) + sizeof (char *), but two steps are required for memory allocation:
Need to allocate a piece of memory space for the structure; allocate memory space for the member variables in the structure
Memory request:
If ((p_buffer = (struct point_buffer *) malloc (sizeof (struct point_buffer))! = NULL)
{
Pumped buffer-> len = CUR_LENGTH
If ((paired buffer-> data = (char *) malloc (sizeof (char) * CUR_LENGTH))! = NULL)
{
Memcpy (paired buffer-> data, "point_buffer test", CUR_LENGTH)
Printf ("% d,% s\ n", paired buffer-> len, pumped buffer-> data)
}
}
Memory release:
Free (paired buffer-> data)
Free (p_buffer)
P_buffer = NULL
Although this can save memory, the memory allocated twice is discontinuous and needs to be managed separately, resulting in the need to apply for and release memory for the structure and data respectively. this is undoubtedly a disaster for programmers, because it can easily lead to memory leaks caused by forgetting and releasing memory.
Is there a better way? That's today's topic flexible array.
2 what is a flexible array?
Flexible array members (flexible array member) are also called scalable array members, and this code structure arises from the need for dynamic structures. In daily programming, it is sometimes necessary to store a dynamic string in the structure, and in view of the important role of this code structure, C99 even includes it in the standard:
As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member.
Flexible arrays are introduced by the C99 standard, so when your compiler prompts for unsupported syntax, check to see if you have enabled the C99 option or higher.
The definition of C99 standard is as follows:
Struct test {
Short len; / / must have at least one other member
Char arr []; / / flexible array must be the last member of the structure (but also other types, such as int, double,...).
}
Flexible array members must be defined in the structure and are the last element; flexible array members cannot be alone in the structure; flexible arrays do not occupy memory.
At the end of a structure, declaring an array of empty length makes the structure variable in length. For the compiler, an array of length 0 does not take up space, because the array name itself does not take up space, it is just an offset, and the symbol of the array name itself represents an unmodifiable address constant.
But for the size of this array, we can allocate it dynamically. For the compiler, the array name is just a symbol, it doesn't take up any space, it's in the structure, it just represents an offset, represents an unmodifiable address constant!
For this feature of flexible array, it is easy to construct into structures, such as buffers, data packets and so on. In fact, flexible array members have their special use in implementing jump tables, and flexible array members are also used in Redis's SDS data structure and in the implementation of jump tables. Its main purpose is to meet the need for variable-length structures, to solve the problem of memory redundancy and array out-of-bounds when using arrays.
Flexible array examples of solving introduction / / flexible array
Struct soft_buffer
{
Int len
Char data [0]
}
Data structure size = sizeof (struct soft_buffer) = sizeof (int), such variable-length arrays are often used in network communications to construct variable-length packets without wasting space and network traffic.
Apply for memory:
If ((softbuffer = (struct soft_buffer *) malloc (sizeof (struct soft_buffer) + sizeof (char) * CUR_LENGTH)! = NULL)
{
Softbuffer- > len = CUR_LENGTH
Memcpy (softbuffer- > data, "softbuffer test", CUR_LENGTH)
Printf ("% d,% s\ n", softbuffer- > len, softbuffer- > data)
}
Free memory:
Free (softbuffer)
Softbuffer = NULL
Comparing the use of pointers with flexible arrays shows the advantages of using flexible arrays:
Because the pointer address of the structure is discontiguous (twice malloc), the flexible array address is continuous, so only one malloc is needed, the former needs to be released twice, and the latter can be released together. When copying data, when the structure uses pointers, the memory it points to must be copied. Memory discontiguity will be a problem, and flexible arrays can be copied directly. Reduce memory fragmentation, because the flexible array of structures and the addresses of structure members are contiguous, you can apply for memory together, so memory fragmentation can be 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.
Disadvantages: there are requirements for the structure and physique, it is necessary to put it at the end, not the only member.
At this point, the study of "what is the C++ flexible array" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.