当数组大小太大时,C++ 程序停止工作 [英] C++ program stops working when the array size is too large

查看:54
本文介绍了当数组大小太大时,C++ 程序停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MVC++ 2010 中用 C++ 编写了一段代码.在其中,程序迭代一维指针数组(双 *)的元素.但是,当我使输入(指针数组的大小)非常大(例如 15000)并运行程序时,它停止工作并显示一个窗口以关闭程序,因为它没有响应!有什么问题?

I've written a code in c++ in MVC++ 2010. Inside it, the program iterates over elements of a 1-D pointer array (double *). However, when I make the input (size of the pointer array) very large, for example 15000, and run the program, it stops working and shows a window to close the program since it is not responding! What's the problem?

这是构建我正在谈论的数组的部分代码:

Here is part of the code that builds the array I'm talking about:

map<int, double *> CF;
CoefficientMap(CF);

double *T = new double[I * J];
for (int r = 1; r <= I * J; ++r)
    T[r] = 100;
SOR(T, CF, 1.8);

这里是迭代器函数:

void SOR(double *T, map<int, double *> &CF, double w)
{
int iter = 0;
cout << "Stage 2: Solving the linear system of equations using SOR method... ";

const double tol = 0.00001;
double error = tol + 1;

double *TOld = new double[I * J];
for (int i = 1; i <= I * J; ++i)
    TOld[i] = 100;

while (abs(error) > tol)
{
    ++iter;
    for (int i = 1; i <= I * J; ++i)
        T[i] = (CF[i][0] + CF[i][1] * T[i + 1] + CF[i][2] * T[i + J] + CF[i][3] * T[i - J] + CF[i][4] * T[i - 1]) * w + (1 - w) * T[i];

    error = errorCalc(TOld, T, I * J);

    for (int i = 1; i <= I * J; ++i)
        TOld[i] = T[i];

    if (iter % 100 == 0)
    {
        cout << endl << endl;
        cout << "100 iterations done, please wait..." << endl << "Total accumulative error till this point: " << error << endl;
    }

    if (iter > 10000)
        return;
}

cout << "Done!" << endl << endl;
cout << "Converged after " << iter << " iterations!" << endl;
cout << "Final accumulative error: " << error << endl << endl;

}

现在,当 (I * J) 变得足够大时(例如 15000),程序停止工作!

Now, when (I * J) gets large enough (15000 for example) the program stops working!

推荐答案

最可能的解释是,您用完了堆栈空间.简单的解决方法是使数组静态或全局.您也可以使用堆中的 new 分配它.两者都将数组移出堆栈.

Most likely explanation is, you run out of stack space. Simple fix is to make the array static or global. You could also allocate it with new from heap. Both move the array out of stack.

最好的方法可能是使用智能指针并将其放入堆中:

Best is probably to use smart pointer and put it to heap:

std::unique_ptr<double[]> arrayOfDoubles(new double[size]);

当智能指针变量超出范围时,这将负责释放内存,无需手动删除.

That will take care of freeing the memory when smart pointer variable goes out of scope, no need for manual delete.

为了更好的答案,编辑问题以包含代码...

For better answer, edit question to contain code...

您添加的代码至少在数组索引方面存在问题.索引从 0 开始,到数组大小减 1.正确的循环:

Your added code has at least problem with array indexing. Indexes start at 0 and go to array size minus one. Correct loop:

double *T = new double[I * J];
for (int r = 0; r < I * J; ++r)
    T[r] = 100;

你在其他循环中也有同样的错误,同样的修复.

You have same error in other loops too, same fix.

替代解决方案:如果您想从 1 开始索引(例如,因为您以这种方式编写了伪代码算法并且不想更改索引),最简单的方法是分配一个更大的数组而不使用索引 0:

Alternative fix: If you want to start indexing from 1 (like, because you have pseudocode algorithm written that way and don't want to change indexing), simplest is to allocate one bigger array and not use index 0:

double *T = new double[I * J + 1];

有了它,您就可以使用当前的循环.

With that you can use your current loops.

这种由一个数组元素造成的缓冲区溢出是令人讨厌的,因为通常在分配的内存块末尾可能有未使用的空间,因此在您更改数组大小和未使用的空间消失之前,错误可能会完全被忽视.即使溢出导致堆损坏,它也可能会被忽视,直到您更改代码和损坏的效果更改.因此,例如,如果您不走运,添加调试代码可能会隐藏问题.

Such buffer overruns by one array element are nasty, because often there may be unused space at the end of allocated memory block, so bug may go totally unnoticed until you change array size and unused space disappears. And even if overrun results in heap corruption, it might go unnoticed, until you change code and effect of corruption changes. So for example adding debugging code may hide the problem if you are unlucky.

这篇关于当数组大小太大时,C++ 程序停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆