The piece of code:
#include <array>
#include <iostream>
int function()
{
const int N=10000000;
std::array<double, N> array{0.0};
std::cout<<"N="<<N<<std::endl;
return 0;
}
int main(int, char **)
{
function();
exit(0);
}
When I launch the program, I see:
Segmentation fault (core dumped)
The program works only atN<10000000
. I understand that the reason is the overflow of the stack. But if I were to declare the array static:
static std::array<double, N>{0.0};
everything works well up to N=1000000000
. I was surprised.
As far as I can understand, the static std::array
/ std::vector
inside a function is allocated in global memory (as if it was a static global array), not on the stack. That is why I can declare a static array inside a function that is much bigger than an ordinary array local to the function. Is it true?