The last member in the struct is the usage of the [0] length array: This is a common technique commonly used to form buffers. Compared to pointers, with an empty array has such advantages: (1), do not need to initialize, the array name is directly in the offset, (2), do not occupy any space, the pointer needs to occupy an int length space, an empty array does not occupy any space. "This array does not occupy any memory", meaning that such a structure saves space; "The memory address of the array is the same as the address of the element behind it", meaning that the array name is the address of the subsequent element without initialization, and can be used directly as a pointer.
This is the best way to make a dynamic buffer, because it can be allocated space malloc (sizeof (STRUCTXXX) + Buff_len); The structure of the buffer is directly allocated with a block of buffers. It is also very convenient to use, because now the empty array has actually become an array of buff_len lengths. Such benefits are: (1), a distribution to solve the problem, save a lot of trouble. To prevent memory leaks, if the second malloc fails, the first allocated struct must be rolled back if it is divided by two allocations (struct and buffer). This brings a coding problem. Second, after assigning the second buffer, if the structure uses a pointer, the pointer is also assigned a value. Also, in the free buffer, use the pointer two times free. If an empty array is used, all problems are resolved at once. (2), small memory management is very difficult, if the pointer, the struct part of the buffer is a small memory, in the system is more than bound to seriously affect the performance of memory management. There is no problem if the struct and the actual data buffer are allocated large chunks at once using an empty array. Thus, using an empty array simplifies coding and solves the problem of small memory fragmentation that improves performance.
The reason why structs end up using a 0 or 1 length array is primarily to facilitate the management of memory buffers (in fact, to allocate a contiguous amount of memory, to reduce the fragmentation of memory), if you are using pointers directly instead of arrays, you must allocate the structure once when allocating memory buffers. Then assign the body of the structure of the pointer once, (and the memory allocated at this time is not contiguous with the structure of the memory, all to be managed to request and release) and if the use of arrays, then only one time can be fully allocated, in turn, the same as the release, the use of arrays, a release. With pointers, you have to release pointers in the structure, release the structure, and not reverse the order.
The last member in the struct is the usage of the [1] length array: The same as the usage of the array of length [0], which is rewritten as [1] for portability. Some compilers do not support [0] arrays, which can be changed to [] or [1].
Incomplete type (incomplete type): it lacks sufficient information such as length to describe a complete object. (1), forward declaration is a common type of incomplete, class base; struct test; Base and test only give a declaration, not a definition. Incomplete types must be supplemented in some way in order to be instantiated using them, otherwise they can only be used to define pointers or references, because the pointer or reference itself, not the base or test object, is instantiated at this time. (2), an array of unknown lengths also belongs to the incomplete type: extern int a[]; extern cannot be removed because the length of the array is unknown and cannot appear as a definition. An array of incomplete types can be supplemented in several ways in order to be used, and the initialization of curly braces is one of the ways: int a[] = {10, 20};
Flexible array members (flexible array member): Also known as a shrinking array member, this code structure arises from the need for dynamic structures. C99 uses an incomplete type to implement a flexible array member, in C99, the last element in the struct allows an array of unknown size, which is called a flexible array member. However, a flexible array member in a structure must precede at least one other member. Flexible array members allow a variable-sized array to be included in the structure. A flexible array member exists only as a symbolic address and must be the last member of the struct, and the size of the structure returned by SIZEFO does not include the memory of the flexible array. Flexible array members can be used not only for character arrays, but also for arrays of elements of other types. Structures that contain flexible array members are dynamically allocated with 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.
The C + + standard specifies that an array of length 0 cannot be defined, so some compilers use 0-length array members as their own non-standard extensions.
Example code:
[CPP]View Plaincopy
- #include <iostream>
- Using namespace std;
- typedef struct _flexiblearray
- {
- Char ch;
- int arr[0]; //int Arr[];//int arr[1];
- }flexiblearray;
- int main ()
- {
- cout<<sizeof (Flexiblearray) <<endl;
- const int LENGTH = 10;
- flexiblearray* Flexiblearray = (flexiblearray*)new char[sizeof (flexiblearray) + LENGTH * sizeof ( int)];
- For (int i = 0; i < LENGTH; i + +) {
- Flexiblearray->arr[i] = i * i;
- }
- For (int i = 0; i < LENGTH; i + +) {
- cout<<flexiblearray->arr[i]<<endl;
- }
- Delete [] flexiblearray;
- return 0;
- }
Reference documents:
1, http://blog.chinaunix.net/uid-26750459-id-3191136.html
2, http://blog.csdn.net/ce123_zhouwei/article/details/8973073
3, http://blog.csdn.net/code_crash/article/details/4854939
The last member in the struct is a [0] or [1] length Array (flexible array member) usage