How do you dynamically allocate memory for an std::array (C++11)

I've been trying to find the syntax for this.

int * myPointer;
myPointer = new int[randomVariable];

Like that but for std::array.
Last edited on
A std::array must have a size known at compile time. You should use a std::vector instead.

std::vector myVector(randomVariable);
closed account (3qX21hU5)
C++ doesn't support VLA's (Variable Length Arrays), like shacktar said the arrays size must be know at compile time. In the up and coming version of C++ I believe are going to implement something that resembles C's VLA's which will be called dynarray (I could be mistaken on the name).

So basically you can't. As suggested use std::vector which will handle this for you and remove a whole lot of possibilities for bugs in your code.
Topic archived. No new replies allowed.