2 ms·
A beautiful, and simple, addition to the C language would be a mechanism to assess the remaining stack size. This is trivial to check in assembler, but practic
by enriquto 9y ago
A beautiful, and simple, addition to the C language would be a mechanism to assess the remaining stack size. This is trivial to check in assembler, but practically impossible in portable C.
Indeed, thanks to VLA, the stack can be used as a clean notation for raii. You just replace this ugly, but very common, construction
void f(int n)
{
float *x = malloc(n * sizeof*x);
// ...
free(x);
}
with this
void f(int n)
{
float x[n];
// ...
}
- BruceIV 9y ago[on the Cforall team] we actually take advantage of this same VLA idiom for temporary storage for polymorphic variables. Fun fact from when we switched our implementation to use VLAs: if you call alloca() for stack memory instead, it doesn't work properly inside a loop, because it never releases the memory until the stack frame is exited.
- pjmlp 9y agoVLAs were made optional in C11 as they became yet another security exploit vector, and not all C vendors are keen in supporting it, especially on the embedded space.