C++中使用全局动态数组的代码在排序函数中停止。

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

C++ Code with Global Dynamic Array Stops in the Sort Function

问题

我一直在尝试编写一个用户定义的数组,然后最终对其进行排序,但当代码达到排序函数时,代码最终停止。 代码如下:

  1. #include <iostream>
  2. using namespace std;
  3. int array_size;
  4. int* array = new int[array_size];
  5. void create_array_cmd()
  6. {
  7. cout << "请键入所需的数组大小:";
  8. cin >> array_size;
  9. cout << "请输入您希望保存的数字字符:" << endl;
  10. for (int i = 0; i < array_size; i++)
  11. {
  12. cout << "数组 [" << i + 1 << "]: ";
  13. cin >> ::array[i];
  14. }
  15. }
  16. void print_values_cmd()
  17. {
  18. cout << "这是您输入的值:" << endl;
  19. for (int i = 0; i < array_size; i++)
  20. {
  21. cout << ::array[i] << " ";
  22. }
  23. cout << endl;
  24. }
  25. void array_sort_cmd()
  26. {
  27. cout << "排序后的值为:" << endl;
  28. for (int i = 1; i < array_size; i++)
  29. {
  30. for (int j = i + 1; j < array_size; j++)
  31. {
  32. int k = ::array[i];
  33. j = i - 1;
  34. while (j >= 0 && ::array[j] > k)
  35. {
  36. ::array[j + 1] = ::array[j];
  37. j = j - 1;
  38. }
  39. ::array[j+1] = k;
  40. }
  41. }
  42. for (int i = 0; i < array_size; i++)
  43. {
  44. cout << ::array[i] << " ";
  45. }
  46. }
  47. int main()
  48. {
  49. create_array_cmd();
  50. print_values_cmd();
  51. array_sort_cmd();
  52. }

此外,当我在主函数的最后添加以下内容:

  1. delete array[];

代码将不再工作。不知何故,代码将正常工作,直到您输入多于2个值。

英文:

I've been trying to code a user-defined array then eventually sort it out but the code eventually stops when it reaches the sort function. The code is as follows:

  1. #include &lt;iostream&gt;
  2. using namespace std;
  3. int array_size;
  4. int* array = new int[array_size];
  5. void create_array_cmd()
  6. {
  7. cout &lt;&lt; &quot;Please input your desired array size: &quot;;
  8. cin &gt;&gt; array_size;
  9. cout &lt;&lt; &quot;Please key-in the numeric characters you wish to save: &quot; &lt;&lt; endl;
  10. for (int i = 0; i &lt; array_size; i++)
  11. {
  12. cout &lt;&lt; &quot;Array [&quot; &lt;&lt; i + 1 &lt;&lt; &quot;]: &quot;;
  13. cin &gt;&gt; ::array[i];
  14. }
  15. }
  16. void print_values_cmd()
  17. {
  18. cout &lt;&lt; &quot;Here are the values you entered: &quot; &lt;&lt; endl;
  19. for (int i = 0; i &lt; array_size; i++)
  20. {
  21. cout &lt;&lt; ::array[i] &lt;&lt; &quot; &quot;;
  22. }
  23. cout &lt;&lt; endl;
  24. }
  25. void array_sort_cmd()
  26. {
  27. cout &lt;&lt; &quot;The sorted values are: &quot; &lt;&lt; endl;
  28. for (int i = 1; i &lt; array_size; i++)
  29. {
  30. for (int j = i + 1; j &lt; array_size; j++)
  31. {
  32. int k = ::array[i];
  33. j = i - 1;
  34. while (j &gt;= 0 &amp;&amp; ::array[j] &gt; k)
  35. {
  36. ::array[j + 1] = ::array[j];
  37. j = j - 1;
  38. }
  39. ::array[j+1] = k;
  40. }
  41. }
  42. for (int i = 0; i &lt; array_size; i++)
  43. {
  44. cout &lt;&lt; ::array[i] &lt;&lt; &quot; &quot;;
  45. }
  46. }
  47. int main()
  48. {
  49. create_array_cmd();
  50. print_values_cmd();
  51. array_sort_cmd();
  52. }

Also, when I add

  1. delete array[];

at the very end of the main function, the code won't work anymore. Somehow, the code will work fine until you input more than 2 values.

答案1

得分: 1

arrayarray_size 被声明为全局变量,并在开头进行初始化。因此,在尝试为 array 分配内存时,array_size 的值将为 0

您可以在请求用户输入后分配内存,或者更好地使用 std::vector<int>

英文:

array and array_size are declared as global variables and initialized at the beginning. So array_size will have value 0 when you are trying to allocate memory for array.

You can allocate memory after asking for user input or better still to use std::vector&lt;int&gt;

huangapple
  • 本文由 发表于 2023年2月27日 12:48:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576854.html
匿名

发表评论

匿名网友

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

确定