Flexible array member (reprint)

Source: Internet
Author: User

http://blog.csdn.net/code_crash/article/details/4854939

Incomplete type

C is the same as C + + with respect to the semantics of incomplete types. An incomplete type is a type that lacks sufficient information such as length to describe a complete object.

A forward declaration is a common, incomplete type:

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.

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

The flexible array member (flexible array member) is also known as a scalable array member, and its appearance reflects the extreme pursuit of the refined code by C programmers.

This code structure arises from the need for dynamic structures. In daily programming, it is sometimes necessary to store a string of dynamic length in a struct, and the general practice is to define a pointer member in the struct that points to the dynamic memory space where the string resides, for example:

struct Test {

int A;

Double b;

Char *p;

};

P points to a string. This method causes the string to be separated from the struct, and is not conducive to manipulation, if the string is linked directly to the struct, is not it better? Therefore, you can modify the code to this:

Char a[] = "Hello World";

struct Test *pnttest = (struct test*) malloc (sizeof (struct test) + strlen (a) + 1);

strcpy (Pnttest + 1, a);

In this way, (char*) (Pnttest + 1) is the address of the string "Hello World". At this point P becomes superfluous and can be removed.

However, another problem arises: the constant Use (char*) (Pnttest + 1) is inconvenient.

If you can find a method that can directly reference the string, without taking up the space of the structure, it is perfect, the code structure that conforms to this condition should be a non-object symbolic address, placing a 0-length array at the tail of the struct is a wonderful solution.

However, the C + + standard does not define an array of length 0, so some compilers use 0-length array members as their own non-standard extensions, for example:

struct Test {

int A;

Double b;

Char c[0];

};

c is called a flexible array member, if the pnttest point to the dynamic allocation of memory as a whole, C is a length can dynamically change the structure of the member, the word flexible is derived from this.

The length of c is 0, so it does not occupy the test space, and Pnttest->c is the first address of "Hello World", no need to use (char*) (pnttest + 1) so ugly syntax.

In view of the important role of this code structure, C99 even put it into the standard. C99 uses an incomplete type to implement a flexible array member, which is the standard form:

struct Test {

int A;

Double b;

Char c[];

};

C also does not occupy test space, only as a symbolic address exists, and must be the last member of the struct. Flexible array members can be used not only for character arrays, but also for elements of other types of arrays, such as:

struct Test {

int A;

Double b;

float c[];

};

The standard form should be used as much as possible, and the pointer method can be used in non-C99 situations. Some people use char a[1], which is very undesirable, to take such a as a flexible array member will occur out of bounds behavior, although the C + + standard does not stipulate that the compiler should check out of bounds, but there is no rule can not check out of bounds, for a small pointer space to sacrifice portability, is not worth it.

Flexible array member (reprint)

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.