Eighth flexible Array Members

Source: Internet
Author: User

Before you tell a flexible array member, first introduce the incomplete type (incomplete type). An incomplete type is a type that lacks sufficient information such as length to describe a complete object.

6.2.5 Types

Incomplete Types (Types that describe objects but lack information needed to determine their sizes).

C is the same as C + + with respect to the semantics of incomplete types.

There are basically no books about incomplete types, and many people have a blank head when they first encounter this concept. In fact, we often use incomplete types in actual engineering design, but we do not know that there is such a concept. 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};

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.

Given the important role that this code structure produces, C99 even earns it the standard:

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 has an incomplete array type; This is called a flexible array member.

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.

Eighth flexible Array Members

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.