C语言 写一个函数给 malloc 双指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19525265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a function to malloc double pointer
提问by Ace
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?
为什么这不起作用?
回答by Some programmer dude
You need one more "indirection", in other words pass Gby reference like a pointer to a pointer to a pointer to float:
您还需要一个“间接”,换句话说,通过G引用传递,就像指向指向浮点指针的指针一样:
void mallocDoubleArr(double ***arr, int size);
And then call it as
然后将其称为
mallocDoubleArr(&G, numNodes);
Modify mallocDoubleArraccordingly, like for example
相应地修改mallocDoubleArr,例如
(*arr)[i] = malloc(size*sizeof(double));
回答by bstamour
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. There is no difference in this regard.
在我看来,对于使用指针的初学者来说,很多困惑来自认为它们与常规的旧变量不同。指针,如整数、浮点数等,只是存在于堆栈中的变量:它们有地址,并且以相同的方式传递给函数。如果要更改函数中的变量(int、float、指针等),则需要将指针传递给它。在这方面没有区别。
回答by Mark Plotnick
C is call-by-value. In
C 是按值调用。在
double **G;
mallocDoubleArr(G, numNodes);
you are passing an uninitialized variable to mallocDoubleArr. It may be zero, it may be something else, but it almost certainly isn't something that mallocDoubleArr can assign to.
您将一个未初始化的变量传递给 mallocDoubleArr。它可能是零,也可能是其他东西,但几乎可以肯定它不是 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. 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**.
我们可以更改代码和函数的定义,以传入 G 的地址,但是您正在处理另一个级别的指针。这可能会使代码更难理解。如果这真的不是必需的,我建议改为让 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. Sorry for any overlap.]
[编辑:bstamour 的帖子是在我写我的时候发的。抱歉有任何重叠。]
回答by mehmet riza oz
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);

