From User's Guide to gnu c ++
Zero-length arrays are allowed in gnu c ++. They are very useful as the last element of a structure which is really a header for a variable-length object:
Struct line {
Int length;
Char contents [0];
};
{
Struct line * thisline = (struct line *) malloc (sizeof (struct line) + this_length );
Thisline-> length = this_length;
}
In standard C, you wowould have to give contents a length of 1, which means either you waste space or complicate the argument to malloc.
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //
Resolution:
The idea of a zero-length array comes from the C language-trying to explain a variable-length struct. In the preceding example, the size of struct line is four bytes for the compiler (the length of the INT-type field ). However, you can dynamically allocate, for example, a memory block of 200 bytes (type: Char [200]), and then cast the pointer of this block into a pointer of the type struct line. In this way, in the struct line variable, the field contents can be considered as a char array with a length of 196 elements.
The advantage of this approach is that it is more efficient than specifying contents as a char pointer. This is because no indirect access is required to access the contents element.
The disadvantage of this approach is that it is not safe, especially when releasing this block (200 bytes), you need to "manually" intervene. The compiler considers that the block pointed by the struct line pointer has only four bytes. That is, you should not release struct line, but instead release the first 200 bytes of memory block dynamically allocated.
However, in the C ++ language, a zero-length array is not allowed (from ppbench, C ++ standard ISO/IEC-14882 (2003) e)
Therefore, the "feature" of gnu c ++ cannot be transplanted, or it does not need to be good (or the length of the array must be at least 1 ).
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////