简体   繁体   English

为什么我在Visual Studio中遇到堆栈溢出错误

[英]Why I'm getting stack overflow error in Visual Studio

I'm a beginner in C++ and don't understand many of background processes such as how compiler allocates memory etc. I'm using Visual Studio 2013 for writing c++ program. 我是C ++的初学者,不了解许多后台进程,例如编译器如何分配内存等。我正在使用Visual Studio 2013编写c ++程序。 I have to use visual studio as I'm linking matlab to my c++ code. 我必须使用Visual Studio,因为我要将Matlab链接到我的C ++代码。

I am getting stack overflow error which I don't know how to remove it. 我收到了堆栈溢出错误,我不知道如何删除它。 I don't get this error when I set N = 100, but when I set larger values of N such as 150, 200, I get the stack overflow error. 当我将N设置为100时,我没有收到此错误,但是当我设置较大的N值(例如150、200)时,我得到了堆栈溢出错误。

It will be really great if anyone can point out why I'm getting this error in VS. 如果有人能指出为什么我在VS中遇到此错误,那将是非常不错的。 I tested the same code using codeblocks with GCC compiler which doesn't give any error at all. 我在GCC编译器中使用代码块测试了相同的代码,这完全没有给出任何错误。

#include<iostream>
using namespace std;

const int N = 200;              //Number of grid points

int main()
{

double n = N;
double dx = 4 / (n - 1); double dy = 4 / (n - 1);
double col = n, row = n;
double dt = 0.5*dx;                             // time step size(dTau)
int itmax = 2500;                               // max iterations
int i, j, t;
double x[N] = { 0.0 }, y[N] = { 0.0 };
x[0] = -2; y[0] = -2;                           // define grid
for (i = 1; i < N; i++){
    x[i] = x[i - 1] + dx;
    y[i] = y[i - 1] + dy;
}

// Initialize variables
double phi_0[N][N], phi_new[N][N],  F_0[N][N], F_new[N][N], F[N][N],
    Fext[N][N], phi[N][N];

return 0;
}

By default, Visual Studio creates a stack that's only a few megabytes (or maybe only one--I don't remember exactly, but typically only a small fraction of the memory on a current machine anyway). 默认情况下,Visual Studio创建的堆栈只有几兆字节(或者也许只有一个-我不记得了,但是无论如何通常通常只占当前计算机的一小部分内存)。 When N gets too large, you're just using more than that. 当N太大时,您将使用更多。

One obvious alternative is to define your large arrays as static so they won't be on the stack. 一种明显的选择是将大型数组定义为static这样它们就不会出现在堆栈中。

static double phi_0[N][N], phi_new[N][N],  F_0[N][N], F_new[N][N], F[N][N],
    Fext[N][N], phi[N][N];

This forces the arrays to be allocated statically. 这将强制对数组进行静态分配。 In main this makes little real difference, because main isn't allowed to be called recursively. main这几乎没有什么真正的区别,因为不允许main递归调用。 In a function that was directly or indirectly recursive, however, this could/would cause a problem. 但是,在直接或间接递归的函数中,这可能/将引起问题。 In such a case, you'd probably want to use an std::vector instead, as this allocates (most of) its data on the free store and uses only a small, fixed amount of stack memory (typically around 12-32 bytes per vector). 在这种情况下,您可能要使用std::vector ,因为这会在免费存储区中分配(大部分)数据,并且仅使用少量固定的堆栈内存(通常约为12-32字节)每个向量)。

You're allocating 7 double [N][N] variables with automatic storage duration ie on stack. 您要分配7 double [N][N]具有自动存储持续时间的double [N][N]变量,即在堆栈上。 A single double variable is of size 64-bits (usually) ie 8 bytes; 一个双精度变量的大小(通常)为64位,即8个字节。 so in total 8 * 200 * 200 * 7 = 2240000 bytes (2.13 MiB ) are required. 因此总共需要8 * 200 * 200 * 7 = 2240000字节(2.13 MiB )。 This is probably more than the stack space allowed for a frame on your platform. 这可能超过平台上框架允许的堆栈空间。

MSDN says that the default stack size is 1 MiB while you're trying to allocate more than that, hence the error. MSDN说 ,当您尝试分配更多的默认堆栈大小时,默认堆栈大小为1 MiB,因此出现错误。

Apart from the alternative that Jerry suggests, you also have the option of dynamic, free-store (usually on the heap) allocation, which is not limited by the stack size. 除了Jerry建议的替代方案之外,您还可以选择动态的,自由存储(通常在堆上)分配,该分配不受堆栈大小的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM