Remix.run Logo
▲ randomNumber7 5 hours ago

As far as I know many C programmers think the addition of variable length arrays in C99 was a mistake. What would be the downside of this approach?

▲pjmlp 5 hours ago | parent | next [-]

So much so, that it was made optional annex in C11, while Google paid to remove all their use from the Linux kernel.

▲ActorNightly 25 minutes ago | parent | prev | next [-]

VLAs are anti C. I also dont even think _Generics should be in the complier. The complier should do minimal processing of syntax in terms of decision making, with only focus on being optimizing code.

Because in the flip side, you get C++ templates, which are turing complete, so you can write entire program syntax that the compliler executies during the complie process.

▲uecker 5 hours ago | parent | prev [-]

I do not get the connection with a VLA in this context, but if you need a VLA, by all means use it. It is basically always better than the alternative.

▲randomNumber7 5 hours ago | parent [-]

> I do not get the connection with a VLA in this context

The article uses a struct with a VLA as the last element.

▲uecker 5 hours ago | parent [-]

I see. This array of unknown length at the end of a struct is called a flexible array member and not a variable length array (although it also refers to an array of variable length it is a different language feature).

▲lukasgelbmann 4 hours ago | parent | next [-]

Yes, and variable length arrays were made an optional part of the standard in C11. Whereas flexible array members remain a standard required feature.

Flexible array members don’t allocate a dynamic amount of memory on the stack.

▲uecker 4 hours ago | parent | next [-]

VLAs are supported by basically all modern C compilers though, and I do not think adding them was a mistake (I certainly use them a lot!). They got a bad reputation due to stack clash attacks, but in the past some compilers did not implement stack probing (clang was very late). But this was fixed a decade ago.

▲david2ndaccount 2 hours ago | parent [-]

Their specification is pretty bad, allowing code like:

    #include <stdio.h>
    #include <stddef.h>
    void v(int y[printf("hello world!\n")]){ 
    }

    int main(){
        v(NULL);
        typedef int wut[printf("oh no\n")];
    }
    $ cc vla.c && ./a.out
    hello world!
    oh no
`typedef`s with side effects...
▲ 4 hours ago | parent | prev [-]
[deleted]
▲randomNumber7 4 hours ago | parent | prev [-]

What happens when I put a variable of a struct type that contains a flexible array member on the stack?

▲uecker 3 hours ago | parent [-]

The array has size 0. In GNU C, you can initialize it and then it has a fixed size corresponding to the initializer.