A Brief Introduction to flexible arrays in C Language

Source: Internet
Author: User

A Brief Introduction to flexible arrays in C Language

In daily programming, sometimes you need to store a dynamic string in the struct. The general practice is to define a pointer member in the struct, the pointer member points to the dynamic memory space of the string, for example:

typedef struct test  {         int a;         double b;         char *p;  };

P points to a string. This method causes the string to be separated from the struct, which is not conducive to operation. Isn't it better to directly connect a string with a struct? Therefore, you can modify the Code as follows:

char a[] = "hello world";test *stpTest = (test *)malloc(sizeof(test) + strlen( a ) + 1 );strcpy(stpTest + 1, a );

In this way, (char *) (stptest + 1) is the address of the string "Hello World. At this time, p becomes a superfluous thing and can be removed. However, another problem arises: it is always inconvenient to use (char *) (stptest + 1. If you can find a method that can directly reference the string without occupying the space of the struct, it is perfect. The code structure that meets this condition should be a non-object symbolic address, placing a zero-length array at the end of the struct is a wonderful solution. However, the C/C ++ standard does not define an array with a length of 0. Therefore, some compilers use an array with a length of 0 as their ownNon-standard extension.
Before describing flexible array members, we should first introduce the incomplete type (incomplete type ). The incomplete type is such a type. It lacks sufficient information such as the length to describe a complete object. Its appearance reflects the ultimate pursuit of refined code by C programmers, this code structure comes from the need for dynamic structures.

In view of the important role of this code structure, c99 even included it in the standard. C99 uses an incomplete type to implement flexible array members. In c99, the last element in the structure can be an array of unknown sizes, which is called a flexible array) A member (also called a scalable array member), but a flexible array member in the structure must have at least one other member before it. Flexible array Members allow the structure to contain an array of variable sizes. Flexible array members only exist as one symbolic address and must be the last member of the struct. The size of the structure returned by sizeof does not include the memory of the flexible array. Flexible array members can be used not only for character arrays, but also for arrays whose elements are of other types. The structure containing flexible array members uses the malloc () function for dynamic memory allocation,And the allocated memory should be larger than the structure size to adapt to the expected size of the flexible array.. For the use of flexible arrays, see the following example:

typedef struct test{int a;double b;char c[0];};

Some compilers may report errors and cannot compile them. You can change them:

typedef struct test{int a;double b;char c[];};

Use the following expression to allocate memory to the struct:

test *stpRest = (test *)malloc(sizeof(test)+100*sizeof(char));

C is a flexible array member. If we regard the Dynamic Allocation memory pointed to by stptest as a whole, C is a struct member whose length can be changed dynamically. The word flexibility comes from this. The length of C is 0, so it does not occupy the space of test. At the same time, stptest-> C is the first address of "Hello World" and does not need to be used (char *) (stptest + 1) so ugly code. The array with zero elements does not occupy space. Then we can perform the variable length operation. In this way, we allocate a piece of memory for the struct pointer C. With stptest-> C [N], you can easily access variable-length elements.

Of course, since the above uses the malloc function to allocate memory, you must use the free function to release the memory:

free(stpTest);

The standard format should be used whenever possible. Instead of c99, the pointer method can be used. It should be noted that c89 does not support such a thing. c99 adds it as a special case to the standard. However, c99 supports incomplete type, instead of zero array, which is equivalent to int A [0]. This form is invalid, and c99 supports the same form as int A []; some compilers use int A [0];Non-standard extensionAnd the non-standard extension has been available before the c99 release. After the c99 release, Some compilers combine the two.

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.