Array with zero length

Source: Internet
Author: User
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 ).

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.