Variable length arrays (C++ only)

http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frzarg%2Fvariable_length_arrays.htm

Variable length arrays (C++ only)

A variable length array, which is a C99 feature, is an array of automatic storage duration whose length is determined at run time.

Read syntax diagram

Skip visual syntax diagram

Variable length array declarator syntax
 
>>-array_identifier--[--+-expression-------------+--]----------><
                        '-+-----------------+--*-'
                          '-type-qualifiers-'
 

If the size of the array is indicated by * instead of an expression, the variable length array is considered to be of unspecified size. Such arrays are considered complete types, but can only be used in declarations of function prototype scope.

A variable length array and a pointer to a variable length array are considered variably modified types. Declarations of variably modified types must be at either block scope or function prototype scope. Array objects declared with the extern storage class specifier cannot be of variable length array type. Array objects declared with the static storage class specifier can be a pointer to a variable length array, but not an actual variable length array. A variable length array cannot be initialized.

Note:
In C++ applications, storage allocated for use by variable length arrays is not released until the function they reside in completes execution.

A variable length array can be the operand of a sizeof expression. In this case, the operand is evaluated at run time, and the size is neither an integer constant nor a constant expression, even though the size of each instance of a variable array does not change during its lifetime.

A variable length array can be used in a typedef statement. The typedef name will have only block scope. The length of the array is fixed when the typedef name is defined, not each time it is used.

A function parameter can be a variable length array. The necessary size expressions must be provided in the function definition. The compiler evaluates the size expression of a variably modified parameter on entry to the function. For a function declared with a variable length array as a parameter, as in the following,

void f(int x, int a[][x]);

the size of the variable length array argument must match that of the function definition.

The C++ extension does not include support for references to a variable length array type; neither may a function parameter be a reference to a variable length array type.

Related information

  • Flexible array members

原文地址:https://www.cnblogs.com/shangdawei/p/3098806.html