[C, C ++]-use an array of 0 elements to implement a variable-size struct

Source: Internet
Author: User

Source: http://hi.baidu.com/zhoutianyang/blog/item/1eab2d1f57cc1bcda7866995.html

 

# Include < Stdio. h >
# Include < String . H >
# Include < Stdlib. h >
Struct AA {
Int A;
Int B;
};

StructBb {
StructAA test [0];
};

Int Main ( Void )
{
Struct Bb * P = ( Struct Bb * ) Malloc ( Sizeof ( Struct Bb) + Sizeof ( Struct Aa) * 100 );
P -> Test [ 0 ]. = 10 ;
P -> Test [ 0 ]. B = 20 ;
Printf ( " % D, % d \ n " , P -> Test [ 0 ]. A, P -> Test [ 0 ]. B );
Return 0 ;
}

 

 

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 zero elements does not occupy space. 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, after allocating more memory than sizeof (type_a), int item []; has its meaning. It points to int ncnt; the following content does not need memory, it is a very useful technique to control the memory allocated during allocation.
Release is also simple:

 

// C language:
Free (P );
// C ++ language version:
Delete [] P;

This is called the flexible/elastic array member (fleible array member) c89 does not support this kind of thing, and c99 adds it as a special case to the standard. However, c99 supports incomplete type, instead of zero array, which is equivalent to int item [0]. This form is invalid, and c99 supports the same form as int item []; some compilers only support int item [0]; as non-standard extensions, and have such non-standard extensions 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, the 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. (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 the array shall remain that of the flexible array Member, even if this wowould differ from that of the replacement array. if this array wowould have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

Note that the "variable length array" added by c99 is distinguished ":
The c89 Standard specifies that the array size must be determined at the compilation time. In c99, this standard item is extended and can be a value determined at the runtime. That is to say, the variable length array has nothing to do with C ++ itself. As long as c99 is supported, the variable length array can be used, including the C compiler that supports c99.

It should be noted that the dimensions of the variable-length array remain unchanged during the lifetime of the array. That is to say, the variable-length array is not dynamic and the variable is only the size of the array.

This feature is introduced to support numerical processing.

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.