为什么此代码上升为EXCEPTION_STATK_OVERFLOW

Why this code raise to EXCEPTION_STACK_OVERFLOW?

本文关键字:EXCEPTION STATK OVERFLOW 代码 为什么      更新时间:2023-10-16

所以我写代码。

我不明白为什么会出现异常_STACK_OVERFLOW?

#include <iostream>
using namespace std;
int main(){
    char data[2048][2048] = {{0}};
    cout << "test";
    return 0;
}

即使我没有将私有化

char data[2048][2048];

两起案件的情况相同。

Running "main.exe", press ESC to terminate...        
Crash                                                
  EXCEPTION_STACK_OVERFLOW                           
  time consumed: 0.01 sec                            
  time passed:   0.08 sec                            
  peak memory:   4395008 bytes                       

您的变量太大,无法将其保留在堆栈中。您应该使用动态存储持续时间。

std::unique_ptr<char[]> data(new char[2048*2048]);

然而,如果您真的想要或必须将其保留在堆栈中,这里有一个gcc标志来更改默认堆栈大小:

--堆栈,4194304其中4194304是以字节为单位的堆栈大小