2D内存分配

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

2D memory allocation

问题

我正在尝试修改我的程序,

我遇到的问题实际上是字符串数组的分配。

尝试运行以下程序时,我遇到了以下错误 -

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. void function_manipulate(unsigned int **arr_str, unsigned int *arr_len)
  5. {
  6. *arr_len = 3;
  7. int len = *arr_len;
  8. // *arr_str = (unsigned char *)malloc(sizeof(unsigned char *)*(*arr_len));
  9. *arr_str = new unsigned int *[len];
  10. for (int i = 0; i < len; i++)
  11. {
  12. cout << (*arr_str)[i] << '\n';
  13. }
  14. }
  15. int main()
  16. {
  17. unsigned int *array_string = NULL;
  18. unsigned int array_length = 0;
  19. function_manipulate(&array_string, &array_length);
  20. }

输出结果如下:

  1. 0
  2. 0
  3. 0
英文:

I am trying to modify my program,

The prblem I am running into is actually the array of string allocation.

When trying to run the below program, I am getting the below error -

  1. #include &lt;iostream&gt;
  2. #include &lt;cstdlib&gt;
  3. using namespace std;
  4. void function_manipulate(unsigned int **arr_str, unsigned int *arr_len)
  5. {
  6. *arr_len = 3;
  7. int len = *arr_len;
  8. // *arr_str = (unsigned char *)malloc(sizeof(unsigned char *)*(*arr_len));
  9. arr_str = new unsigned int *[len];
  10. for (int i = 0; i &lt; len; i++)
  11. {
  12. cout &lt;&lt; arr_str[i] &lt;&lt;&#39;\n&#39;;
  13. }
  14. }
  15. int main()
  16. {
  17. unsigned int *array_string = NULL;
  18. unsigned int array_length = 0;
  19. function_manipulate(&amp;array_string, &amp;array_length);
  20. }

The output looks like:

  1. 0
  2. 0
  3. 0

答案1

得分: 0

我认为这是您试图做的事情。

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. void function_manipulate(unsigned int **arr_str, unsigned int& arr_len)
  5. {
  6. arr_len = 3;
  7. int len = arr_len;
  8. *arr_str = new unsigned int [len];
  9. for (int i = 0; i < len; i++)
  10. {
  11. (*arr_str)[i] = 1234;
  12. }
  13. }
  14. int main()
  15. {
  16. unsigned int *array_string = NULL;
  17. unsigned int array_length = 0;
  18. function_manipulate(&array_string, array_length);
  19. if (array_string != NULL){
  20. cout << "array_length now is " << array_length << endl;
  21. for (int i = 0; i < array_length; i++)
  22. {
  23. cout << array_string[i] << '\n';
  24. }
  25. delete[] array_string;
  26. }
  27. return 0;
  28. }

请注意,这段代码相当丑陋,我不建议这样做,但是它确实可以工作。

英文:

I think this is what you are trying to do.

  1. #include &lt;iostream&gt;
  2. #include &lt;cstdlib&gt;
  3. using namespace std;
  4. void function_manipulate(unsigned int **arr_str, unsigned int&amp; arr_len)
  5. {
  6. arr_len = 3;
  7. int len = arr_len;
  8. // *arr_str = (unsigned char *)malloc(sizeof(unsigned char *)*(*arr_len));
  9. *arr_str = new unsigned int [len];
  10. for (int i = 0; i &lt; len; i++)
  11. {
  12. (*arr_str)[i] = 1234;
  13. //cout &lt;&lt; arr_str[i] &lt;&lt;&#39;\n&#39;;
  14. }
  15. }
  16. int main()
  17. {
  18. unsigned int *array_string = NULL;
  19. unsigned int array_length = 0;
  20. function_manipulate(&amp;array_string, array_length);
  21. if (array_string != NULL){
  22. cout &lt;&lt; &quot;array_length now is &quot; &lt;&lt; array_length &lt;&lt; endl;
  23. for (int i = 0; i &lt; array_length; i++)
  24. {
  25. cout &lt;&lt; array_string[i] &lt;&lt;&#39;\n&#39;;
  26. }
  27. delete[] array_string;
  28. }
  29. return 0;
  30. }

Note that this is pretty ugly and I would not recommend it. But it can be done.

huangapple
  • 本文由 发表于 2023年7月18日 12:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709615.html
匿名

发表评论

匿名网友

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

确定