Flexible Array Structure in C ++

Source: Internet
Author: User

The last element in the structure of C99 is an array of unknown size. It is called a flexible data structure member, but the flexible array member in the structure must have at least one other member before it. A flexible array Member may contain an array of variable sizes. The sizeof returned size of this structure does not contain the memory of flexible arrays. The structure containing flexible array members uses malloc 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. -An example of using a flexible array from C language Daquan is as follows: # include <stdio. h> // define a flexible array typedef struct StType {int count; int item [0];} StType; int main () {printf ("sizeof (StType) = % d \ n ", (int) sizeof (StType); int val [4] = {3, 1, 2, 3}; StType * st = (StType *) val; for (int I = 0; I <st-> count; I ++) printf ("% d value = % d \ n", I, st-> item [I]); return 0;} after compilation, the running result is as follows: sizeof (StType) = 4 0 value = 1 1 value = 2 2 value = 3 from the preceding result It can be seen that item [0] does not occupy space, so we can use malloc or new to perform the variable length operation, as shown below. StType * st = (StType *) new char [sizeof (StType) + 100 * sizeof (int)]; st. count = 100; delete [] st; the above Code produces a StType object of 100 items. You can use delete [] st; to release the allocated memory. This flexible array member (Fleiblearray member) is not supported in C89. C99 is added to the standard as a special case, but C99 supports incomplete types instead of 0 arrays, the use of int item [0]; definition is invalid. You need to use int item [] to replace it. Some compilers can use int item [0] as non-standard. Note: The warning information will be prompted during compilation in VC, as follows: warning C4200: nonstandard extension used: zero-sized array in struct/union Cannot generate copy-ctor orcopy-assignment operator when UDT contains a zero-sized array differentiate between the "variable length array" added by C99: C89 standard, 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. Sometimes people will consider compatibility issues that do not exist for the moment. If we port the program to a platform with only the standard C89 compiler, there are several alternative methods below: 1) method 1 struct header {size_t len; unsigned char data [1] ;}; ptr = malloc (sizeof (struct header) + (n-1 )); however, whether this method works depends on implementation, which breaks the portability rules. 2) method 2 struct header * my_header = malloc (offsetof (struct header, data) + n * sizeof (my_header-> data); or use a macro to simplify the Code: # define FLEXIBLE_SIZE SIZE_MAX // or whatever maximum length for an array # defineSIZEOF_FLEXIBLE (type, member, length) \ (offsetof (type, member) + (length) * sizeof (type *) 0)-> member [0]) This method is too complex and does not seem to have any special benefits to maintain C89 compatibility, you can use an elastic array member without a complete C99 compiler. 3) method 3: Use pointers. Do not save that indirect reference. If portability is so important.

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.