As my usually used C++ compilers allow variable-length arrays (eg. arrays depending on runtime size), I wonder if there is something like std::array with variable size? Of course std::vectoris of variable size, but it allocates on heap, and reallocates on need.
I like to have a stack allocated array with size defined at runtime. Is there any std-template that may feature this? Maybe using std::vector with a fixed maximal size?
std::array, the size of the array is a template parameter, so it cannot be a runtime variable. I guessstd::vectoris your best bet. - Daniel Kamil Kozarstd::vectorwith a custom allocator. Since you expect the data to be located "on the stack", presumably they will always be freed in reverse order they're allocated. It should be quite easy to write an extremely fast thread-local allocator given that restriction. The downside is that you will make a separate allocation up front to store the data, but this isn't all that different from what the OS does for your thread's stack - on a modern OS your block is virtual-only until used, just like the stack. But it won't benefit from the stack being hot in cache. - Steve Jessop