Comment by randomNumber7
7 hours ago
> I do not get the connection with a VLA in this context
The article uses a struct with a VLA as the last element.
7 hours ago
> I do not get the connection with a VLA in this context
The article uses a struct with a VLA as the last element.
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).
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.
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.
1 reply →
What happens when I put a variable of a struct type that contains a flexible array member on the stack?
The array has size 0. In GNU C, you can initialize it and then it has a fixed size corresponding to the initializer.