As my usually used C++ compilers allow variable-length arrays (e.g. arrays depending on runtime size), I wonder if there is something like std::array with variable size? Of course std::vector is 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 maximum size?
As Daniel stated in the comment, size of the
std::arrayis specified as a template parameter, so it cannot be set during runtime.You can though construct
std::vectorby passing the minimum capacity through the constructor parameter:But. Still vector's contents will be stored on the heap, not on the stack. The problem is, that compiler has to know, how much space should be allocated for the function prior to its execution, so it is simply not possible to store variable-length data on the stack.