3 ms·
I don't get how there is a meaningful difference with regard to failure between a VLA on the stack and malloc'ing a buffer on the heap. In both cases you have t
by binarybanana 5y ago
I don't get how there is a meaningful difference with regard to failure between a VLA on the stack and malloc'ing a buffer on the heap. In both cases you have to do a check if you want to avoid a crash on resource exhaustion. The only difference is that stack size is not standardized among implementaions, but IIRC you can get/set it appropriately for your needs with POSIX APIs (I think pthreads has such a function?). Even on Linux you can disable overcommit if you want to reliably handle OOM in user space and if you don't the malloc case is actually less reliable since it will never fail anyway.
Also I wonder how VLAs compare to alloca? It's basically malloc, but on the stack and I've seen a lot of software use it, for example nnn and picom make allocations with it. Wouldn't the same issues also affect alloca? I mean an array is just a contiguous memory buffer in C, so alloca could be used to back an array of variable size in the same way, or is there something I'm missing?
But then, neither is the size of RAM, or whatever limits are in place.