7 ms·
I've been looking into Ada recently and it has cool safety mechanisms to encourage this same kind of thing. It even allows you to dynamically allocate on the st
by thisoneisreal 8mo ago
I've been looking into Ada recently and it has cool safety mechanisms to encourage this same kind of thing. It even allows you to dynamically allocate on the stack for many cases.
- apaprocki 8mo agoYou can allocate dynamically on the stack in C as well. Every compiler will give you some form of alloca().
- adrian_b 8mo agoTrue, but in many environments where C is used the stacks may be configured with small sizes and without the possibility of being grown dynamically. In such environments, it may be needed to estimate the maximum stack usage and configure big enough stacks, if possible. Having to estimate maximum memory usage is the same constraint when allocating a static array as a work area, then using a custom allocator to provide memory when needed.
- apaprocki 8mo agoSure, the parent was commenting more about the capability existing in Ada in contrast to C. Ada variable length local variables are basically C alloca(). The interesting part in Ada is returning variable length types from functions and having them automatically managed via the “secondary stack”, which is a fixed size buffer in embedded/constrained environments. The compiler takes care of most of the dirty work for you. We mainly use C++, not C, and we do this with polymorphic allocators. This is our main allocator for local stack: https://bloomberg.github.io/bde-resources/doxygen/bde_api_prod/group__bdlma__localsequentialallocator.html https://bloomberg.github.io/bde-resources/doxygen/bde_api_pr... … or this for supplying a large external static buffer: https://bloomberg.github.io/bde-resources/doxygen/bde_api_prod/group__bdlma__bufferedsequentialallocator.html https://bloomberg.github.io/bde-resources/doxygen/bde_api_pr...
- lelanthran 8mo ago> You can allocate dynamically on the stack in C as well. Every compiler will give you some form of alloca(). And if it doesn't, VLAs are still in there until C23, IIRC.