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

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

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次迭代时不起作用吗?

  1. #include <iostream>
  2. #include "wes.hpp"
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <fstream>
  8. int main()
  9. {
  10. srand(time(NULL));
  11. const int num_E = 100000; // 迭代次数
  12. double E1_max = 2000.0 * 1000; double E1_min = 300.0 * 1000;
  13. double E2_max = 100.0 * 1000; double E2_min = 1.0 * 1000;
  14. double E3_max = 100.0 * 1000; double E3_min = 1.0 * 1000;
  15. int num_H = 100.0;
  16. double H1_max = 12; double H1_min = 3;
  17. double H2_max = 25; double H2_min = 17;
  18. int num_L6000 = 100000;
  19. double L6000_max = 7000; double L6000_min = 5000;
  20. // 为E声明矩阵
  21. double E1[num_E]; double E2[num_E]; double E3[num_E];
  22. double E4[num_E]; double E5[num_E];
  23. // 为H声明矩阵
  24. double H1[num_E]; double H2[num_E]; double H3[num_E];
  25. double H4[num_E]; double H5[num_E];
  26. // 为q声明矩阵
  27. double q[num_E];
  28. // 变量说明
  29. double u1 = 0.35; double u2 = 0.45; double u3 = 0.45; double u4 = 0.45; double u5 = 0.45;
  30. double la1 = 1; double la2 = 1; double la3 = 1;
  31. int l = 1;
  32. double p[] = { 0 };
  33. double a[] = { 5.9 };
  34. double xc[] = { 0 };
  35. double yc[] = { 0 };
  36. int l1 = 1;
  37. int ls = 1;
  38. double xs[] = { 0 };
  39. double ys[9] = {};
  40. double zs[] = { 0 };
  41. double results[900];
  42. double X[9] = { 0, 8, 12, 18, 24, 36, 48, 60, 72 };
  43. for (int i = 0; i < num_E; i++) // 生成E的随机数据
  44. {
  45. int num_items_E1 = E1_max - E1_min + 1;
  46. int num_items_E2 = E2_max - E2_min + 1;
  47. int num_items_E3 = E3_max - E3_min + 1;
  48. E1[i] = E1_min + rand() % num_items_E1;
  49. E2[i] = E2_min + rand() % num_items_E2;
  50. E3[i] = E3_min + rand() % num_items_E3;
  51. E4[i] = E3[i];
  52. E5[i] = E3[i];
  53. }
  54. for (int i = 0; i < num_E; i++) // 生成H的随机数据
  55. {
  56. int num_items_H1 = H1_max - H1_min + 1;
  57. int num_items_H2 = H2_max - H2_min + 1;
  58. H1[i] = H1_min + rand() % num_items_H1;
  59. H2[i] = H2_min + rand() % num_items_H2;
  60. H3[i] = 999;
  61. H4[i] = 999;
  62. H5[i] = 999;
  63. }
  64. for (int i = 0; i < num_E; i++) // 生成q的随机数据
  65. {
  66. int num_items_load = L6000_max - L6000_min + 1;
  67. double load = L6000_min + rand() % num_items_load;
  68. q[i] = load / (3.141652 * pow(a[0], 2));
  69. }
  70. std::fstream my_file;
  71. my_file.open("Output.csv");
  72. my_file << "E1" << "," << "E2" << "," << "E3" << "," <<
  73. "H1" << "," << "H2" << "," << "H3" << "," <<
  74. "q" << "," << "D0" << "," << "D1" << "," << "D2" << "," << "D3" << "," << "D4" << "," <<
  75. "D5" << "," << "D6" << "," << "D7" << "," << "D8" << "\n";
  76. // 运行主程序
  77. for (int i = 0; i < num_E; i++) {
  78. double e1 = E1[i]; double e2 = E2[i]; double e3 = E3[i]; double e4 = E4[i]; double e5 = E5[i];
  79. double h1 = H1[i]; double h2 = H2[i]; double h3 = H3[i]; double h4 = H4[i]; double h5 = H5[i];
  80. p[0] = q[i];
  81. for (int j = 0; j < 9; j++) {
  82. ys[0] = X[j];
  83. wes5(&e1, &e2, &e3, &e4, &e5,
  84. &h1, &h2, &h3, &h4, &u1, &u2, &u3, &u4, &u5,
  85. &la1, &la2, &la3, &l,
  86. p, a, xc, yc,
  87. &l1, &ls, xs, ys, zs,
  88. results);
  89. if (j == 0) {
  90. my_file << E
  91. <details>
  92. <summary>英文:</summary>
  93. 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.
  94. However, when I increase the iteration number to 100,000, it shows the stack overflow error:
  95. &quot;Unhandled exception at 0x00007FF6E76D8408 in ConsoleApplication.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000ABB1693000).&quot;
  96. The name of the function is wes5.cpp which is a void function. It stores the calculated results in the &quot;results&quot; array.
  97. 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?
  98. #include &lt;iostream&gt;
  99. #include &quot;wes.hpp&quot;
  100. #include &lt;cstdlib&gt;
  101. #include &lt;ctime&gt;
  102. #include &lt;cmath&gt;
  103. #include &lt;iostream&gt;
  104. #include &lt;fstream&gt;
  105. int main()
  106. {
  107. srand(time(NULL));
  108. const int num_E = 100000; // number of itteration
  109. double E1_max = 2000.0 * 1000; double E1_min = 300.0 * 1000;
  110. double E2_max = 100.0 * 1000; double E2_min = 1.0 * 1000;
  111. double E3_max = 100.0 * 1000; double E3_min = 1.0 * 1000;
  112. int num_H = 100.0;
  113. double H1_max = 12; double H1_min = 3;
  114. double H2_max = 25; double H2_min = 17;
  115. int num_L6000 = 100000;
  116. double L6000_max = 7000; double L6000_min = 5000;
  117. //matrix declration for E
  118. double E1[num_E]; double E2[num_E]; double E3[num_E];
  119. double E4[num_E];double E5[num_E];
  120. //matrix declration for H
  121. double H1[num_E]; double H2[num_E]; double H3[num_E];
  122. double H4[num_E];double H5[num_E];
  123. //matrix declration for q
  124. double q[num_E];
  125. // variable description
  126. double u1 = 0.35; double u2 = 0.45; double u3 = 0.45; double u4 = 0.45; double u5 = 0.45;
  127. double la1 = 1; double la2 = 1; double la3 = 1;
  128. int l = 1;
  129. double p[] = { 0 };
  130. double a[] = { 5.9 };
  131. double xc[] = { 0 };
  132. double yc[] = { 0 };
  133. int l1 = 1;
  134. int ls = 1;
  135. double xs[] = { 0 };
  136. double ys[9] = {};
  137. double zs[] = { 0 };
  138. double results[900];
  139. //double my_results[10]; //num_E
  140. double X[9] = { 0, 8, 12, 18, 24, 36, 48, 60, 72 };
  141. for (int i = 0; i &lt; num_E; i++) // Random data generation for E
  142. {
  143. int num_items_E1 = E1_max - E1_min + 1;
  144. int num_items_E2 = E2_max - E2_min + 1;
  145. int num_items_E3 = E3_max - E3_min + 1;
  146. E1[i] = E1_min + rand() % num_items_E1;
  147. E2[i] = E2_min + rand() % num_items_E2;
  148. E3[i] = E3_min + rand() % num_items_E3;
  149. E4[i] = E3[i];
  150. E5[i] = E3[i];
  151. }
  152. for (int i = 0; i &lt; num_E; i++) // Random data generation for H
  153. {
  154. int num_items_H1 = H1_max - H1_min + 1;
  155. int num_items_H2 = H2_max - H2_min + 1;
  156. H1[i] = H1_min + rand() % num_items_H1;
  157. H2[i] = H2_min + rand() % num_items_H2;
  158. H3[i] = 999;
  159. H4[i] = 999;
  160. H5[i] = 999;
  161. }
  162. for (int i = 0; i &lt; num_E; i++) // Random data generation for q
  163. {
  164. int num_items_load = L6000_max - L6000_min + 1;
  165. double load = L6000_min + rand() % num_items_load;
  166. q[i] = load / (3.141652 * pow(a[0], 2));
  167. }
  168. std::fstream my_file;
  169. my_file.open(&quot;Output.csv&quot;);
  170. 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;
  171. &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;
  172. &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;
  173. &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;;
  174. // running the main program
  175. for (int i = 0; i &lt; num_E; i++) {
  176. double e1 = E1[i]; double e2 = E2[i]; double e3 = E3[i]; double e4 = E4[i]; double e5 = E5[i];
  177. double h1 = H1[i]; double h2 = H2[i]; double h3 = H3[i]; double h4 = H4[i]; double h5 = H5[i];
  178. p[0] = q[i];
  179. for (int j = 0; j &lt; 9; j++) {
  180. ys[0] = X[j];
  181. wes5(&amp;e1, &amp;e2, &amp;e3, &amp;e4, &amp;e5,
  182. &amp;h1, &amp;h2, &amp;h3, &amp;h4, &amp;u1, &amp;u2, &amp;u3, &amp;u4, &amp;u5,
  183. &amp;la1, &amp;la2, &amp;la3, &amp;l,
  184. p, a, xc, yc,
  185. &amp;l1, &amp;ls, xs, ys, zs,
  186. results);
  187. //my_results[i] = results[700];
  188. if (j == 0) {
  189. 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;
  190. H1[i] &lt;&lt; &quot;,&quot; &lt;&lt; H2[i] &lt;&lt; &quot;,&quot; &lt;&lt; H3[i] &lt;&lt; &quot;,&quot; &lt;&lt;
  191. p[0] &lt;&lt; &quot;,&quot; &lt;&lt; results[700] &lt;&lt; &quot;,&quot;;
  192. }
  193. else {
  194. my_file &lt;&lt; results[700] &lt;&lt; &quot;,&quot;;
  195. }
  196. }
  197. my_file &lt;&lt; &quot;\n&quot;;
  198. }
  199. my_file.close();
  200. }
  201. </details>
  202. # 答案1
  203. **得分**: 2
  204. 根据您的错误信息,您正在使用Visual Studio,它的默认堆栈大小为1MB。在您的代码中,您分配了10个包含100,000个双精度浮点数的数组。因此,每个数组的大小为800,000字节(双精度浮点数占8字节)。将这个乘以10,您的数组占用了8MB的堆栈大小。这就是您遇到堆栈溢出错误的原因。
  205. 如果将大小设置为10,000,您的数组将使用0.8MB的堆栈空间,这足够小,可以正常工作。
  206. 您可以使用[`/F`](https://learn.microsoft.com/en-us/cpp/build/reference/f-set-stack-size?view=vs-2019)选项来更改堆栈大小,但我建议重新考虑您的代码。也许使用`vector`会更好?
  207. <details>
  208. <summary>英文:</summary>
  209. 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.
  210. With size 10,000 you use 0.8MB of stack for your arrays, which is small enough to be able to still work.
  211. 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?
  212. </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:

确定