如何在C++中使用数组时防止栈溢出?

huangapple go评论76阅读模式
英文:

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&#39;m writing a program in C++ that calls a function in each iteration of a for-loop and stores the &quot;results&quot; 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: 

&quot;Unhandled exception at 0x00007FF6E76D8408 in ConsoleApplication.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000ABB1693000).&quot;

The name of the function is wes5.cpp which is a void function. It stores the calculated results in the &quot;results&quot; 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 &lt;iostream&gt;
    #include &quot;wes.hpp&quot;
    #include &lt;cstdlib&gt;
    #include &lt;ctime&gt;
    #include &lt;cmath&gt;
    #include &lt;iostream&gt;
    #include &lt;fstream&gt;
    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 &lt; 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 &lt; 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 &lt; 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(&quot;Output.csv&quot;);
    	my_file &lt;&lt; &quot;E1&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;E2&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;E3&quot; &lt;&lt; &quot;,&quot; &lt;&lt;
    		&quot;H1&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;H2&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;H3&quot; &lt;&lt; &quot;,&quot; &lt;&lt;
    		&quot;q&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;D0&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;D1&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;D2&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;D3&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;D4&quot; &lt;&lt; &quot;,&quot; &lt;&lt;
    		&quot;D5&quot;&lt;&lt; &quot;,&quot; &lt;&lt; &quot;D6&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;D7&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;D8&quot; &lt;&lt; &quot;\n&quot;;
    
    	// running the main program
    	for (int i = 0; i &lt; 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 &lt; 9; j++) {
    
    			ys[0] = X[j];
    
    			wes5(&amp;e1, &amp;e2, &amp;e3, &amp;e4, &amp;e5,
    				&amp;h1, &amp;h2, &amp;h3, &amp;h4, &amp;u1, &amp;u2, &amp;u3, &amp;u4, &amp;u5,
    				&amp;la1, &amp;la2, &amp;la3, &amp;l,
    				p, a, xc, yc,
    				&amp;l1, &amp;ls, xs, ys, zs,
    				results);
    
    			//my_results[i] = results[700];
    
    			if (j == 0) {
    				my_file &lt;&lt; E1[i] &lt;&lt; &quot;,&quot; &lt;&lt; E2[i] &lt;&lt; &quot;,&quot; &lt;&lt; E3[i] &lt;&lt; &quot;,&quot; &lt;&lt;
    					H1[i] &lt;&lt; &quot;,&quot; &lt;&lt; H2[i] &lt;&lt; &quot;,&quot; &lt;&lt; H3[i] &lt;&lt; &quot;,&quot; &lt;&lt;
    					p[0] &lt;&lt; &quot;,&quot; &lt;&lt; results[700] &lt;&lt; &quot;,&quot;;
    			}
    			else {
    				my_file &lt;&lt; results[700] &lt;&lt; &quot;,&quot;;
    			}
    
    
    		}
    
    		my_file &lt;&lt; &quot;\n&quot;;
    	
    	}
    	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&#39;d recommend rethinking your code instead. Perhaps a `vector` would be better?

</details>



huangapple
  • 本文由 发表于 2020年1月4日 01:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定