3 ms·
> Alloca is not standard C A similar option is a variable-length array, which is standard C. It does share the problem of not being available in some compilers
by cesarb 10y ago
> Alloca is not standard C
A similar option is a variable-length array, which is standard C. It does share the problem of not being available in some compilers, however.
That said, there are other problems with alloca/VLAs. For small enough sizes, like you said it's faster and simpler to statically allocate. For large enough sizes, you risk overflowing the stack, which might be more constrained than you'd expect (especially when using threads). There might be a sweet spot where it's better, but it's not worth the portability cost and the potential headaches.
- mikeash 10y agoNote that variable-length arrays ended up being a particularly troublesome feature, and were therefore made optional in C11. It's still "standard," but a compliant compiler is allowed not to support them, so it might as well not be.