英文:
How to prevent stack overflow when using array in C++?
问题
我正在用C++编写一个程序,在for循环的每次迭代中调用一个函数,并将"结果"存储在.csv文件中。该函数接受18个参数,并将结果存储在一个数组中。当我将迭代次数增加到100,000时,程序出现堆栈溢出错误:
"Unhandled exception at 0x00007FF6E76D8408 in ConsoleApplication.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000ABB1693000)."
该函数的名称是wes5.cpp,它是一个void函数。它将计算结果存储在"results"数组中。
请注意,函数的一些输入是使用for循环随机生成的。有人能告诉我为什么使用10,000次迭代时程序正常工作,而使用100,000次迭代时不起作用吗?
#include <iostream>
#include "wes.hpp"
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
#include <fstream>
int main()
{
srand(time(NULL));
const int num_E = 100000; // 迭代次数
double E1_max = 2000.0 * 1000; double E1_min = 300.0 * 1000;
double E2_max = 100.0 * 1000; double E2_min = 1.0 * 1000;
double E3_max = 100.0 * 1000; double E3_min = 1.0 * 1000;
int num_H = 100.0;
double H1_max = 12; double H1_min = 3;
double H2_max = 25; double H2_min = 17;
int num_L6000 = 100000;
double L6000_max = 7000; double L6000_min = 5000;
// 为E声明矩阵
double E1[num_E]; double E2[num_E]; double E3[num_E];
double E4[num_E]; double E5[num_E];
// 为H声明矩阵
double H1[num_E]; double H2[num_E]; double H3[num_E];
double H4[num_E]; double H5[num_E];
// 为q声明矩阵
double q[num_E];
// 变量说明
double u1 = 0.35; double u2 = 0.45; double u3 = 0.45; double u4 = 0.45; double u5 = 0.45;
double la1 = 1; double la2 = 1; double la3 = 1;
int l = 1;
double p[] = { 0 };
double a[] = { 5.9 };
double xc[] = { 0 };
double yc[] = { 0 };
int l1 = 1;
int ls = 1;
double xs[] = { 0 };
double ys[9] = {};
double zs[] = { 0 };
double results[900];
double X[9] = { 0, 8, 12, 18, 24, 36, 48, 60, 72 };
for (int i = 0; i < num_E; i++) // 生成E的随机数据
{
int num_items_E1 = E1_max - E1_min + 1;
int num_items_E2 = E2_max - E2_min + 1;
int num_items_E3 = E3_max - E3_min + 1;
E1[i] = E1_min + rand() % num_items_E1;
E2[i] = E2_min + rand() % num_items_E2;
E3[i] = E3_min + rand() % num_items_E3;
E4[i] = E3[i];
E5[i] = E3[i];
}
for (int i = 0; i < num_E; i++) // 生成H的随机数据
{
int num_items_H1 = H1_max - H1_min + 1;
int num_items_H2 = H2_max - H2_min + 1;
H1[i] = H1_min + rand() % num_items_H1;
H2[i] = H2_min + rand() % num_items_H2;
H3[i] = 999;
H4[i] = 999;
H5[i] = 999;
}
for (int i = 0; i < num_E; i++) // 生成q的随机数据
{
int num_items_load = L6000_max - L6000_min + 1;
double load = L6000_min + rand() % num_items_load;
q[i] = load / (3.141652 * pow(a[0], 2));
}
std::fstream my_file;
my_file.open("Output.csv");
my_file << "E1" << "," << "E2" << "," << "E3" << "," <<
"H1" << "," << "H2" << "," << "H3" << "," <<
"q" << "," << "D0" << "," << "D1" << "," << "D2" << "," << "D3" << "," << "D4" << "," <<
"D5" << "," << "D6" << "," << "D7" << "," << "D8" << "\n";
// 运行主程序
for (int i = 0; i < num_E; i++) {
double e1 = E1[i]; double e2 = E2[i]; double e3 = E3[i]; double e4 = E4[i]; double e5 = E5[i];
double h1 = H1[i]; double h2 = H2[i]; double h3 = H3[i]; double h4 = H4[i]; double h5 = H5[i];
p[0] = q[i];
for (int j = 0; j < 9; j++) {
ys[0] = X[j];
wes5(&e1, &e2, &e3, &e4, &e5,
&h1, &h2, &h3, &h4, &u1, &u2, &u3, &u4, &u5,
&la1, &la2, &la3, &l,
p, a, xc, yc,
&l1, &ls, xs, ys, zs,
results);
if (j == 0) {
my_file << E
<details>
<summary>英文:</summary>
I'm writing a program in C++ that calls a function in each iteration of a for-loop and stores the "results" in a .csv file. The function takes in 18 parameters and stores the results in an array. When I run the program with 10,000 iterations it works fine.
However, when I increase the iteration number to 100,000, it shows the stack overflow error:
"Unhandled exception at 0x00007FF6E76D8408 in ConsoleApplication.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000ABB1693000)."
The name of the function is wes5.cpp which is a void function. It stores the calculated results in the "results" array.
Note that some of the inputs of the function are randomly generated using for- loop. Can anyone tell me why it works fine with 10000 iterations and does not work with 100,000?
#include <iostream>
#include "wes.hpp"
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
#include <fstream>
int main()
{
srand(time(NULL));
const int num_E = 100000; // number of itteration
double E1_max = 2000.0 * 1000; double E1_min = 300.0 * 1000;
double E2_max = 100.0 * 1000; double E2_min = 1.0 * 1000;
double E3_max = 100.0 * 1000; double E3_min = 1.0 * 1000;
int num_H = 100.0;
double H1_max = 12; double H1_min = 3;
double H2_max = 25; double H2_min = 17;
int num_L6000 = 100000;
double L6000_max = 7000; double L6000_min = 5000;
//matrix declration for E
double E1[num_E]; double E2[num_E]; double E3[num_E];
double E4[num_E];double E5[num_E];
//matrix declration for H
double H1[num_E]; double H2[num_E]; double H3[num_E];
double H4[num_E];double H5[num_E];
//matrix declration for q
double q[num_E];
// variable description
double u1 = 0.35; double u2 = 0.45; double u3 = 0.45; double u4 = 0.45; double u5 = 0.45;
double la1 = 1; double la2 = 1; double la3 = 1;
int l = 1;
double p[] = { 0 };
double a[] = { 5.9 };
double xc[] = { 0 };
double yc[] = { 0 };
int l1 = 1;
int ls = 1;
double xs[] = { 0 };
double ys[9] = {};
double zs[] = { 0 };
double results[900];
//double my_results[10]; //num_E
double X[9] = { 0, 8, 12, 18, 24, 36, 48, 60, 72 };
for (int i = 0; i < num_E; i++) // Random data generation for E
{
int num_items_E1 = E1_max - E1_min + 1;
int num_items_E2 = E2_max - E2_min + 1;
int num_items_E3 = E3_max - E3_min + 1;
E1[i] = E1_min + rand() % num_items_E1;
E2[i] = E2_min + rand() % num_items_E2;
E3[i] = E3_min + rand() % num_items_E3;
E4[i] = E3[i];
E5[i] = E3[i];
}
for (int i = 0; i < num_E; i++) // Random data generation for H
{
int num_items_H1 = H1_max - H1_min + 1;
int num_items_H2 = H2_max - H2_min + 1;
H1[i] = H1_min + rand() % num_items_H1;
H2[i] = H2_min + rand() % num_items_H2;
H3[i] = 999;
H4[i] = 999;
H5[i] = 999;
}
for (int i = 0; i < num_E; i++) // Random data generation for q
{
int num_items_load = L6000_max - L6000_min + 1;
double load = L6000_min + rand() % num_items_load;
q[i] = load / (3.141652 * pow(a[0], 2));
}
std::fstream my_file;
my_file.open("Output.csv");
my_file << "E1" << "," << "E2" << "," << "E3" << "," <<
"H1" << "," << "H2" << "," << "H3" << "," <<
"q" << "," << "D0" << "," << "D1" << "," << "D2" << "," << "D3" << "," << "D4" << "," <<
"D5"<< "," << "D6" << "," << "D7" << "," << "D8" << "\n";
// running the main program
for (int i = 0; i < num_E; i++) {
double e1 = E1[i]; double e2 = E2[i]; double e3 = E3[i]; double e4 = E4[i]; double e5 = E5[i];
double h1 = H1[i]; double h2 = H2[i]; double h3 = H3[i]; double h4 = H4[i]; double h5 = H5[i];
p[0] = q[i];
for (int j = 0; j < 9; j++) {
ys[0] = X[j];
wes5(&e1, &e2, &e3, &e4, &e5,
&h1, &h2, &h3, &h4, &u1, &u2, &u3, &u4, &u5,
&la1, &la2, &la3, &l,
p, a, xc, yc,
&l1, &ls, xs, ys, zs,
results);
//my_results[i] = results[700];
if (j == 0) {
my_file << E1[i] << "," << E2[i] << "," << E3[i] << "," <<
H1[i] << "," << H2[i] << "," << H3[i] << "," <<
p[0] << "," << results[700] << ",";
}
else {
my_file << results[700] << ",";
}
}
my_file << "\n";
}
my_file.close();
}
</details>
# 答案1
**得分**: 2
根据您的错误信息,您正在使用Visual Studio,它的默认堆栈大小为1MB。在您的代码中,您分配了10个包含100,000个双精度浮点数的数组。因此,每个数组的大小为800,000字节(双精度浮点数占8字节)。将这个乘以10,您的数组占用了8MB的堆栈大小。这就是您遇到堆栈溢出错误的原因。
如果将大小设置为10,000,您的数组将使用0.8MB的堆栈空间,这足够小,可以正常工作。
您可以使用[`/F`](https://learn.microsoft.com/en-us/cpp/build/reference/f-set-stack-size?view=vs-2019)选项来更改堆栈大小,但我建议重新考虑您的代码。也许使用`vector`会更好?
<details>
<summary>英文:</summary>
Judging by your error, you are using Visual Studio, which has a default stack size of 1MB. In your code, you allocate 10 arrays of 100,000 doubles. Each of these arrays, therefore, is 800,000 bytes (double is 8-byte). Multiply that by 10, and you are up to 8MB of stack size for just your arrays. This is why you are getting a stack overflow error.
With size 10,000 you use 0.8MB of stack for your arrays, which is small enough to be able to still work.
You can use the [`/F`](https://learn.microsoft.com/en-us/cpp/build/reference/f-set-stack-size?view=vs-2019) option to change the stack size, but I'd recommend rethinking your code instead. Perhaps a `vector` would be better?
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论