Initialization of array on heap
This is interesting: Pushing an array into a vector
However, if that doesn't do it for you try the following:
#include <algorithm>
...
const int length = 32;
int stack_array[length] = { 0 ,32, 54, ... }
int* array = new int[length];
std::copy(stack_array, stack_array + length, &array[0]);
This can be accomplished today with the following syntax:
int * myHeapArray = new int [3] {1, 2, 3};
Notice you have to match the size of the structure you're allocating with the size of the initializer-list.
Since I'm replying to a question posted years ago, it is worth mentioning that modern C++ discourages the use of new
, delete
and native (or naked) pointers. The use of handlers such as std::unique_ptr
and std::shared_ptr
are favored instead, since they automatically release the memory they own (check RAII idiom).
In this particular case, std::vector
would provide all such features: heap-allocated data, use of initializer-list (like {1, 2, 3}
), handlers and move semantics among other features.
For stack-allocated arrays you can consider std::array
, should you need them.