Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Variable length arrays (VLAVLAs) are essentially the same as traditional C arrays, the major difference being that they are declared with a size that is not a constant integer expression. A variable length array can be declared as follows:

...

Validate size arguments used in VLA declarations. The solution below ensures the size argument, s, used to allocate vla is in a valid range: 1 to a user-defined constant.

Code Block
bgColor#ccccff
enum { MAX_ARRAY = 1024 };

void func(size_t s) {
   int vla[s];
   /* ... */
}

/* ... */
if (s < MAX_ARRAY && s != 0) {
   func(s);
}
else {
   /* Handle Error */
}
/* ... */

...