Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable-length std::array like

Tags:

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?

like image 245
dronus Avatar asked Dec 31 '13 12:12

dronus


People also ask

How do you make a variable length array in C++?

If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself: int n = 10; double* a = new double[n]; // Don't forget to delete [] a; when you're done!

Are variable length arrays allowed in C++?

Variable-length arrays can not be included natively in C++ because they'll require huge changes in the type system.

What makes an array a variable length array?

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified type that depends on a value (see Dependent type).

Is STD array dynamic size?

Why doesn't std::array allow dynamic sizes? std::array<int,3> global; I thought std::array was a good container until I learned that its size should be known at compile time. I.e. you can't create an array with size n at run time.


2 Answers

There are two proposals currently being worked on to bring run-time fixed size arrays to C++ which may be of interest to you:

  • Runtime-sized arrays with automatic storage duration. This would make runtime sized arrays a language feature (like in C11). So you could do:

      void foo(std::size_t size) {     int arr[size];   } 
  • C++ Dynamic Arrays. This would bring a new container to the library, std::dynarray, which is given a fixed size at construction. It is intended to be optimized to be allocated on the stack when possible.

      void foo(std::size_t size) {     std::dynarray<int> arr(size);   } 

These are both being worked on as part of an Array Extensions Technical Specification, which will be released alongside C++14.

UPDATE: std::dynarray is not implemented yet(25Aug2021).please refer to What is the status on dynarrays?

like image 157
Joseph Mansfield Avatar answered Oct 25 '22 03:10

Joseph Mansfield


As Daniel stated in the comment, size of the std::array is specified as a template parameter, so it cannot be set during runtime.

You can though construct std::vector by passing the minimum capacity through the constructor parameter:

#include <vector>  int main(int argc, char * argv[]) {     std::vector<int> a;     a.reserve(5);     std::cout << a.capacity() << "\n";     std::cout << a.size();      getchar(); } 

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.

like image 44
Spook Avatar answered Oct 25 '22 02:10

Spook