I am allocating a multidimensional valarray of size 2000x2000 and it is working smoothly.
valarray<valarray<int>> D(valarray<int>(-2,2000), 2000);
D[1999][1999] = 2000;
However, if I try to allocate a normal array and access an element, I get segmentation fault.
int A[2000][2000];
A[1999][1999] = 2000;
Both are on the stack, why this difference ?
Like
std::vector, the underlying storage ofstd::valarrayis dynamic, and the size of the object that manages this storage does not depend on the number of elements.This program:
produces this output for me:
If you were to use
std::arrayinstead, you would have problems, though.