...
In this noncompliant code example, a variable-length array of size size is declared. The size is declared as size_t in compliance with recommendation INT01-C. Use rsize_t or size_t for all integer values representing the size of an object.
| Code Block | ||||
|---|---|---|---|---|
| ||||
void func(size_t size) {
int vla[size];
/* ... */
}
/* ... */
|
...
This compliant solution ensures the size argument used to allocate vla is in a valid range (between 1 and a programmer-defined maximum); otherwise, it uses an algorithm that relies on dynamic memory allocation.
| Code Block | ||||
|---|---|---|---|---|
| ||||
enum { MAX_ARRAY = 1024 };
void func(size_t size) {
if (0 < size && size < MAX_ARRAY) {
int vla[size];
/* ... */
} else {
/* Use dynamic allocation */
}
}
/* ... */
|
...