4 ms·
Variable Length Arrays were mandatory in C99, optional in C11, and are mandatory again in C23. I'm curious how these dynamics play out in less popular compiler
by fl0ki 3y ago
Variable Length Arrays were mandatory in C99, optional in C11, and are mandatory again in C23.
I'm curious how these dynamics play out in less popular compilers. If a compiler implemented VLAs in C99, they almost certainly still have that feature for backwards compatibility even after they support C11.
Is there any compiler which appeared on the scene between C11 and C23, and during that window, chose not to support VLAs and thus C99? It's not like C11 itself was very widely adopted, precisely because of the long implementation and industry rollout windows.
- Joker_vD 3y agoUh, are they mandatory again? For these reasons, we propose to make variably-modified types mandatory in C23. VLAs with automatic storage duration remain an optional language feature due to their higher implementation overhead and security concerns on some implementations (i.e. when allocated on the stack and not using stack probing). from N2778 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2778.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2778.pdf)
- fractalb 3y agoThanks for the link. As I understood it, it's just the support for the syntax that is mandatory. > Variable length arrays with automatic storage duration are a conditional feature that implementations need not support
- LegionMammal978 3y agoThe syntax and also the semantics. For instance, you can take the sizeof() a variably-modified type, or the offsetof() one of its fields, and the compiler has to do all the layout calculations implied by the type declaration at runtime. These features are partially what motivated the mandatory support. The only part that is still optional is using such types by value as stack variables (i.e., variables with automatic storage duration).
- actionfromafar 3y agoHow do you create a variable array on the heap? confused
- klodolph 3y agoWith `malloc`, and converting the pointer to the correct type. void function(int n) { int (*arr)[n] = malloc(sizeof(*arr)); }
- actionfromafar 3y agowow, that's wild
- uecker 3y agoThat is exactly how you create any other type of object on the heap.
- actionfromafar 3y agoSnarky or just know a lot? It changes quite a bit how the compiler works. It has to know to make sure malloc gets the array element size argument multiplied in runtime by n. To a mere user it broke my mental shorthand of how a C compiler works.
- Joker_vD 3y ago> It has to know to make sure malloc gets the array element size argument multiplied in runtime by n Um, C compilers already do that with arrays with compile-time lengths. #include <stdio.h> void main(void) { char x[20][30]; printf("%zu\n%zu\n", sizeof(x), sizeof(x[0])); } prints 600 30 so you can have "char *y = malloc(sizeof(x)); memcpy(y, x, sizeof(x));" and it must work since C89 at least. The main problem with VLAs is that they make exact the stack frame size unknown until runtime which complicates function prologues/epilogues but that's the problem in the codegen part of the backend, the semantics machinery is mostly in the place already. P.S. And yes, uecker is a member of the ISO C WG14 and GCC contributor, according to his profile.
- fl0ki 3y agoI'm not tuned in to the nuances here but I note that whoever felt they understood it well enough to update Wikipedia summarized this as "The C23 standard makes VLA types mandatory again. Only creation of VLA objects with automatic storage duration is optional." https://en.wikipedia.org/wiki/Variable-length_array#C99 https://en.wikipedia.org/wiki/Variable-length_array#C99 If that phrasing isn't accurate I'm sure they'd appreciate an edit.
- fractalb 3y agoVLA support is mandatory in C23? I'd like to know the rationale behind this decision. Can you provide any references? Thanks.
- veltas 3y agoIf I understand right, this is mandatory: void example(int n) { printf("%zu", sizeof(int[n])); } void example2(int n, int (*a)[n]) { } This is optional: void example3(int n) { int x[n]; }
- garaetjjte 3y agoPortable code couldn't use VLAs anyway, it was never supported in MSVC.
- uecker 3y agoVLAs were optional in C11 and compilers that supported them in C99 also supported them in C11. The only important compiler not supporting VLAs is MSVC, but this compiler also did not support other features from C99. People using MSVC were stuck using an obsolete version of C for a long time. Recently this changed and MSVC started to support C11 and C17 - skipping C99.