[Post] flexible array structure member

Source: Internet
Author: User

Flexible array structure member
In c99, the last element in the structure can be an array of unknown sizes. This is called a flexible array member, but the flexible array member in the structure must have at least one member before it. Flexible array Members allow the structure to contain an array of variable sizes. The size of the structure returned by sizeof does not include the memory of the flexible array. The structure containing flexible array members uses the malloc () function to dynamically allocate memory, and the allocated memory should be larger than the size of the structure to adapt to the expected size of the flexible array .]
C language Daquan, "flexible array members"

Take a look at the flexible array members in the c99 standard:

An array of 0 elements -- an array of 0 Elements
Sometimes we need to generate a struct to implement a variable length structure. How to implement it?
See the definition of this struct:
Typedef struct st_type
{
Int ncnt;
Int item [0];
} Type_a;
(Some compilers may report errors and cannot compile the statements :)
Typedef struct st_type
{
Int ncnt;
Int item [];
} Type_a;
In this way, we can define a variable length structure. Only 4 is obtained using sizeof (type_a), that is, sizeof (ncnt) = sizeof (INT ).

The array with 0 elements does not occupy space, and then we can perform the variable length operation.
C language:
Type_a * P = (type_a *) malloc (sizeof (type_a) + 100 * sizeof (INT ));
C ++ language version:
Type_a * P = (type_a *) New char [sizeof (type_a) + 100 * sizeof (INT)];
In this way, a 100-long type_a type object is generated. The variable length element can be easily accessed using p-> item [N]. The principle is very simple.

, Allocated more memory than sizeof (type_a) after int item []; has its meaning, it points to int ncnt; followed by the content, is not

Memory is needed, and the memory allocated during allocation can be controlled by it, which is a very useful technique.
Release is also simple:
C language:
Free (P );
C ++ language version:
Delete [] P;
In fact, this is called the flexible array member (fleible array member) c89 does not support this kind of thing, c99 adds it as a special case to the standard. However

Yes, c99 supports incomplete type, instead of zero array, which is equivalent to int item [0]. This form is invalid, and c99 supports

The form is the same as int item []; only some compilers support int item [0]; as non-standard extensions, and before c99 is released

After c99 is released, some compilers combine the two into one.
The following content is related to c99:
6.7.2.1 structure and Union specifiers

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. With two exceptions,

Flexible array member is ignored. First, the size of the structure shall be equal to the offset

Of the last element of an otherwise identical structure that replaces the flexible array Member

With an array of unspecified length.106) Second, when a. (Or->) operator has a left operand

That is (a pointer to) a structure with a flexible array member and the right operand names that

Member, it behaves as if that member were replaced with the longest array (with the same element

Type) That wocould not make the structure larger than the object being accessed; the offset of

Array shall remain that of the flexible array Member, even if this wowould differ from that of

Replacement array. If this array wowould have no elements, it behaves as if it had one element

The behavior is undefined if any attempt is made to access that element or to generate a pointer

One past it.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/todototry/archive/2007/04/11/1560458.aspx

 

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.