简体   繁体   English

写一个函数到malloc双指针

[英]Write a function to malloc double pointer

I need to write a function that creates a double pointer using malloc. 我需要编写一个使用malloc创建双指针的函数。

This is how I declared my double pointer as I normally would: 这就是我通常所说的双指针的方式:

double **G; //Create double pointer to hold 2d matrix

*G = malloc(numNodes * sizeof(double*));
for(i = 0; i < numNodes; i++)
{
    G[i] = malloc(numNodes*sizeof(double));
    for (j = 0; j < numNodes; j++)
    {
        G[i][j] = 0;
    }
}

Now I tried replacing it with: 现在我尝试用以下代替它:

double **G;
mallocDoubleArr(G, numNodes);

With the function being: 功能是:

void mallocDoubleArr(double **arr, int size)
{
    int i, j;

    *arr = malloc(size * sizeof(double*));

    for(i = 0; i < size; i++)
    {
        arr[i]= malloc(size*sizeof(double));
        for (j = 0; j < size; j++)
        {
            arr[i][j] = 0;
        }
    }
}

Why doesn't this work? 为什么这不起作用?

You need one more "indirection", in other words pass G by reference like a pointer to a pointer to a pointer to float: 你需要一个“间接”,换句话说,通过引用传递G ,就像指向指向float的指针的指针一样:

void mallocDoubleArr(double ***arr, int size);

And then call it as 然后将其称为

mallocDoubleArr(&G, numNodes);

Modify mallocDoubleArr accordingly, like for example 相应地修改mallocDoubleArr ,例如

(*arr)[i] = malloc(size*sizeof(double));

C is call-by-value. C是按值调用。 In

double **G;
mallocDoubleArr(G, numNodes);

you are passing an uninitialized variable to mallocDoubleArr. 您正在将未初始化的变量传递给mallocDoubleArr。 It may be zero, it may be something else, but it almost certainly isn't something that mallocDoubleArr can assign to. 它可能是零,它可能是其他东西,但它几乎肯定不是mallocDoubleArr可以分配的东西。

We can change the code, and the function's definition, to pass in G's address, but then you're dealing with yet another level of pointer. 我们可以改变代码和函数的定义来传递G的地址,但是你要处理另一个指针级别。 That might make it harder to understand the code. 这可能会让您更难理解代码。 If that really isn't a requirement, I'd propose instead to have mallocDoubleArr return a double**. 如果这不是一个要求,我建议让mallocDoubleArr返回一个双**。

double **G;
G = mallocDoubleArr(numNodes);

double **mallocDoubleArr(int size)
{
    int i, j;
    double **arr;

    arr = (double **) malloc(size * sizeof(double *));

    /* continue with old code */

    return arr;
}

[edit: bstamour's post was made while I was writing mine. [编辑:bstamour的帖子是在我写作时写的。 Sorry for any overlap.] 对不起任何重叠。]

For starters, you need to change the line 对于初学者,您需要更改线路

*G = malloc(numNodes * sizeof(double*));

to

G = malloc(numNodes * sizeof(double*));

(you can't dereference a pointer safely until you've assigned something to it.) (在你为它指定了东西之前,你不能安全地取消引用指针。)

Secondly, your function modifies the pointer passed in, so you need a pointer to it. 其次,你的函数修改了传入的指针,所以你需要一个指向它的指针。 Your signature should instead by 你的签名应该由

void mallocDoubleArr(double ***arr, int size)

and you will need to add the relevant indirections in your code to access the pointer that the pointer is pointing to. 并且您需要在代码中添加相关的间接,以访问指针指向的指针。

A lot of confusion for beginners working with pointers comes from, in my opinion, thinking that they are something different than regular old variables. 对于使用指针的初学者而言,很多困惑来自于我认为它们与常规旧变量不同的东西。 Pointers, like ints, floats, etc. are just variables that live on the stack: they have addresses, and they are passed to functions the same way. 指针,如整数,浮点数等只是存在于堆栈中的变量:它们具有地址,并且它们以相同的方式传递给函数。 If you want to change a variable (int, float, pointer, etc) in a function, you need to pass a pointer to it. 如果要在函数中更改变量(int,float,pointer等),则需要将指针传递给它。 There is no difference in this regard. 这方面没有区别。

I use for matrix operations code like following for allocating and freeing. 我用于矩阵运算代码,如下所示,用于分配和释放。

int **inputMatrix, i, j;
Grid myGrid = *grid;

inputMatrix = (int *) calloc (myGrid.num_nodes, sizeof(int*));
for(i=0; i < myGrid.num_nodes; i++){
    inputMatrix[i] = calloc(myGrid.num_nodes, sizeof(int));
    for(j=0;j<myGrid.num_nodes;j++){
        inputMatrix[i][j] = 0;
    }
};

for(i=0; i < myGrid.num_nodes; i++){
    free(inputMatrix[i]);
}
free (inputMatrix);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM