Is there a standard macro to check support of variable length arrays in C code? It it enough to check for c99 (__STDC_VERSION__ >= 199901L) in all widely used compilers?
1 Answer
From the C11 specification §6.10.8.3
The following macro names are conditionally defined by the implementation:
[...]
__STDC_NO_VLA__The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types.
So if __STDC_VERSION__ > 201000L you need to check __STDC_NO_VLA__.
Otherwise, if __STDC_VERSION__ >= 199901L VLAs should work, but you'll get a compile time error if the compiler is non-compliant.
1 Comment
Seleznev Anton
Thanks. It looks like what I was looking for. I'll examine it.
int foo[x];orint *foo = malloc(x * sizeof (int));(and later free the storage), depending upon whether VLA suport exists. If VLA support exists, and a programmer is willing to chance a stack overflow, the VLA approach may be faster, but code should be usable (but perhaps not as fast) even without VLA support.