Admin Production ni-theme
Current Publication

Variable-Length Arrays

LabWindows/CVI

Variable-Length Arrays

Per the C99 specification, local array declarations may have arbitrary size expressions. The expressions are no longer required to be constant. LabWindows/CVI determines the number of elements of a variable-length array at run time. Variable-length arrays and pointers to variable-length arrays are variably modified types and you can declare them only within a block or a function scope. You cannot declare global variable-length arrays.

LabWindows/CVI allocates storage on the stack for the variable-length array at run time when the declaration comes into scope and deallocates the storage when the object goes out of scope. Use variable-length arrays as an alternative to malloc.

If you use the sizeof operator with a variable-length array, the operator evaluates only at run time when the length of the array is known.

If the size expression of a variable-length array evaluates to a non-positive number at run time, LabWindows/CVI returns a run-time error.

LabWindows/CVI will not report uninitialized local variable errors for uninitialized variable length arrays.

You can use a goto statement to jump within the scope of a variable-length array, but you cannot jump past a declaration of an object of variably modified type.

Note Note  Because static or global variable-length arrays are illegal, you cannot declare variable-length arrays in the Interactive Execution window.

Example Code

void foo (int n)

{

int bar[n+1];

...

}