10 ms·
Even then last I looked C++ doesn't have it either, it's exclusively a C99 feature. For me the downsides outweigh the upsides of alloca and I usually find othe
by vvanders 6y ago
Even then last I looked C++ doesn't have it either, it's exclusively a C99 feature.
For me the downsides outweigh the upsides of alloca and I usually find other approaches(free list, arenas, etc).
- Someone 6y agoWhen comparing rust and C++, I think it’s unfair to interpret “C++” as a specific standard, because you can’t do the same with rust. So, you would have to compare implementations. AFAIK, in practice, many C++ compilers support alloca for compatibility with gcc (or more specifically, its C library) Also, is alloca part of any C standard? I can’t find evidence for that easily. Also, if so, https://en.wikipedia.org/wiki/Stack-based_memory_allocation#System_interface https://en.wikipedia.org/wiki/Stack-based_memory_allocation#... needs updating. It currently says “The function was present on Unix systems as early as 32/V (1978), but is not part of Standard C or any POSIX standard.” I also don’t see how it could be part of the standard, as that, AFAIK, doesn’t even specify the existence of a call stack (a compiler is free to allocate call frames on the heap, for example)
- vvanders 6y agoMost C++ compilers I've worked wouldn't let you use it unless the compilation unit was compiled as C and then linked externally. In the same way I could totally define alloca in a C symbol and then just extern it into Rust if I wanted to. [edit] Realized I was thinking of VLAs but I think the point still stands.
- Someone 6y agoReading https://www.gnu.org/software/gnulib/manual/html_node/alloca.html https://www.gnu.org/software/gnulib/manual/html_node/alloca...., I think gcc C++ supports alloca on most systems (“There are a few systems where this is not possible: HP-UX systems, and some other platforms when the C++ compiler is used. On these platforms the alloca module provides a malloc based emulation”) VLAs are different, I think.
- gpderetta 6y agoC has standardized dynamically sized automatic arrays which is close enough to alloca. The standard wouldn't concern itself of were the space is actually allocated, just mandate that it is freed when the function terminates.
- kllrnohj 6y agoI've found just applying SSO to the container sufficient. Stack if it's small enough, heap if it isn't. You usually want a limit on your alloca anyway.