2D内存分配

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

2D memory allocation

问题

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

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

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

#include <iostream>
#include <cstdlib>
using namespace std;

void function_manipulate(unsigned int **arr_str, unsigned int *arr_len)
{
    *arr_len = 3;
    int len = *arr_len;

    // *arr_str = (unsigned char *)malloc(sizeof(unsigned char *)*(*arr_len));
    *arr_str = new unsigned int *[len];

    for (int i = 0; i < len; i++)
    {
        cout << (*arr_str)[i] << '\n';
    }
}

int main()
{
    unsigned int *array_string = NULL;
    unsigned int array_length = 0;

    function_manipulate(&array_string, &array_length);
}

输出结果如下:

0
0
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 -

#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

void function_manipulate(unsigned int **arr_str, unsigned int *arr_len)
{
	*arr_len = 3;
	int len = *arr_len;

//	*arr_str = (unsigned char *)malloc(sizeof(unsigned char *)*(*arr_len));
	arr_str = new unsigned int *[len];
	

	for (int i = 0; i &lt; len; i++)
	{
		cout &lt;&lt; arr_str[i] &lt;&lt;&#39;\n&#39;;
	}
}

int main()
{
	unsigned int *array_string = NULL;
	unsigned int array_length = 0;
	
	function_manipulate(&amp;array_string, &amp;array_length);
}

The output looks like:

0
0
0

答案1

得分: 0

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

#include <iostream>
#include <cstdlib>
using namespace std;

void function_manipulate(unsigned int **arr_str, unsigned int& arr_len)
{
    arr_len = 3;
    int len = arr_len;

    *arr_str = new unsigned int [len];

    for (int i = 0; i < len; i++)
    {
        (*arr_str)[i] = 1234;
    }
}

int main()
{
    unsigned int *array_string = NULL;
    unsigned int array_length = 0;
    
    function_manipulate(&array_string, array_length);
    
    if (array_string != NULL){
        cout << "array_length now is " << array_length << endl;
        for (int i = 0; i < array_length; i++)
        {
            cout << array_string[i] << '\n';
        }
        delete[] array_string;
    }
    return 0;
}

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

英文:

I think this is what you are trying to do.

#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

void function_manipulate(unsigned int **arr_str, unsigned int&amp; arr_len)
{
    arr_len = 3;
    int len = arr_len;

//  *arr_str = (unsigned char *)malloc(sizeof(unsigned char *)*(*arr_len));
    *arr_str = new unsigned int [len];
    

    for (int i = 0; i &lt; len; i++)
    {
        (*arr_str)[i] = 1234;
        //cout &lt;&lt; arr_str[i] &lt;&lt;&#39;\n&#39;;
    }
}

int main()
{
    unsigned int *array_string = NULL;
    unsigned int array_length = 0;
    
    function_manipulate(&amp;array_string, array_length);
    
    if (array_string != NULL){
        cout &lt;&lt; &quot;array_length now is &quot; &lt;&lt; array_length &lt;&lt; endl;
        for (int i = 0; i &lt; array_length; i++)
        {
            cout &lt;&lt; array_string[i] &lt;&lt;&#39;\n&#39;;
        }
        delete[] array_string;
    }
    return 0;
}

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:

确定