Variable-length std::array like

33.9k Views Asked by At

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?

3

There are 3 best solutions below

2
On

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?

3
On

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.

0
On

Maybe using std::vector with a fixed maximal size?

If boost is allowed then boost::container::static_vector and boost::container::small_vector are the closest I can think of

boost::container::static_vector<int, 1024> my_array;
boost::container::small_vector<int, 1024> my_vector;

static_vector is a sequence container like boost::container::vector with contiguous storage that can change in size, along with the static allocation, low overhead, and fixed capacity of boost::array.

The size of each object is still fixed but it can be worth it if the number of allocations is significant and/or the item count is small

If the vector can grow beyond the limit then just use boost::container::small_vector. The heap is only touched when the size is larger than the defined limit

small_vector is a vector-like container optimized for the case when it contains few elements. It contains some preallocated elements in-place, which can avoid the use of dynamic storage allocation when the actual number of elements is below that preallocated threshold.


If you use Qt then QVarLengthArray is another way to go:

QVarLengthArray is an attempt to work around this gap in the C++ language. It allocates a certain number of elements on the stack, and if you resize the array to a larger size, it automatically uses the heap instead. Stack allocation has the advantage that it is much faster than heap allocation.

Example:

int myfunc(int n)
{
    QVarLengthArray<int, 1024> array(n + 1);
    ...
    return array[n];
}

Some other similar solutions:


If a 3rd party solution isn't allowed then you can roll your own solution by wrapping std::array in a struct to get a static vector

template<typename T, size_t N> 
struct my_static_vector
{
    explicit static_vector(size_t size) { } // ...
    size_t size() const noexcept { return curr_size; }
    static size_t capacity() const noexcept { return N; }
    T& operator[](size_t pos) { return data[pos]; }
    void push_back(const T& value) { data[curr_size++] = value; }
    // ...
private:
    std::array<typename T, N> data;
    std::size_t curr_size;
}

And if small_vector is required then you can use std::variant to contain both the my_static_vector and the vector

template<typename T, size_t N> 
struct my_small_vector
{
    explicit small_vector(size_t size) { } // ...
    size_t size() const noexcept {
        if (data.index() == 0) {
            return data.get<0>().size();
        } else {
            return data.get<1>().size();
        }
    }
    static size_t capacity() const noexcept {
        if (data.index() == 0) {
            return data.get<0>().capacity();
        } else {
            return data.get<1>().capacity();
        }
    }
    // ...
private:
    std::variant<my_static_vector<T, N>, std::vector<T>> data;
}