更改数组尺寸会导致崩溃C ++

JHa*_*ard 1 c++ stdarray

我的程序中需要一些大数组,并且正在使用size = 16 * 16 * 12 * 12的数组对其进行测试。

然后,我改变了我的程序与运行的大小= 64 * 64 * 12 * 12,并在崩溃前甚至去主

这样的阵列会占用太多内存吗?我试图在笔记本电脑上然后在一些功能更强大的台式机上运行代码,在这两种情况下,它都随着较大的阵列立即崩溃,并且适用于较小的阵列。数组大小由在代码开头声明的const int控制。我用

    std::array<double, (64*64*12*12)>.
Run Code Online (Sandbox Code Playgroud)

提前致谢

更新:我编写的具有相同问题的最小程序如下:

    #include <iostream>
    #include <array>
    using namespace std;
    //declare variables
    using std::array;
    const int size_q=2;
    const int qpoints=size_q*size_q*size_q;
    const int size_k=2;
    const int kpoints=size_k*size_k*size_k;
    const int branches=12;
    const int size_ph=kpoints*branches;
    const int size_Theta=size_ph*size_ph;
    array<double, size_Theta> f_ph(array<double,size_Theta>);


    int main(int argc, char const *argv[])
    {
    array<double, size_Theta> theta1;
    f_ph(theta1);
    cout <<"Done";
    return 0;
    }
    array<double, size_Theta> f_ph(array<double,size_Theta> theta1){
    for(int i=0;i<size_Theta;i++){
    theta1[i]=1.00;
    }
    return theta1;
Run Code Online (Sandbox Code Playgroud)

**Update: Seems like it is indeed the memory, using std::vector the program runs smoothly **

Som*_*ken 5

You're most likely running out of memory on the automatic store ("stack").

You can either use a vector:

#include <vector>
std::vector<double> arr(64*64*12*12);
arr[0]; // access first element
Run Code Online (Sandbox Code Playgroud)

Or you can use a unique_ptr if you dont need the flexibility a vector offers:

#include <memory>
auto arr = std::make_unique<std::array<double, (64*64*12*12)>>();
(*arr)[0]; // access first element
Run Code Online (Sandbox Code Playgroud)