C:在动态分配期间通过引用传递 [英] C: Passing by reference during dynamic allocaion
问题描述
我的代码如下分配和初始化数组(floatalloc2
是用于分配2D数组的函数):
My code is to allocate and initialize the array as follows (floatalloc2
is a function used to allocate the 2D array):
#include <stdio.h>
#include <stdlib.h>
void alloc_arr(int adjwfd, int nx, int nz, float **G, float **L1, float **L2)
{
G = floatalloc2(nx, nz);
switch (adjwfd){
case 1:
L1 = floatalloc2(nx, nz);
break;
case 2:
L2 = floatalloc2(nx, nz);
break;
}
}
void init_arr(int adjwfd, int nx, int nz, float **G, float **L1, float **L2)
{
memset(G[0],0,nx*nz*sizeof(float));
switch (adjwfd){
case 1:
memset(L1[0],0,nx*nz*sizeof(float));
break;
case 2:
memset(L2[0],0,nx*nz*sizeof(float));
break;
}
}
int main(int argc, char *argv[])
{
int adjwfd_P=1, nx=10, nz=10;
float **glob=NULL, **local1=NULL, **local2=NULL;
alloc_arr(adjwfd_P, nx, nz, glob, local1, local2);
init_arr(adjwfd_P, nx, nz, glob, local1, local2);
exit(0);
}
它通过编译.但是当我运行这段代码时,它说错了:
It passes the compilation. But when I run this code, it goes wrong says:
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
但是,我发现如果按如下方式更改alloc_arr
,它将成功运行:
However, I found that if I change the alloc_arr
as follows, it runs successfully:
#include <stdio.h>
#include <stdlib.h>
void alloc_arr(int adjwfd, int nx, int nz, float ***G, float ***L1, float ***L2)
{
*G = floatalloc2(nx, nz);
switch (adjwfd){
case 1:
*L1 = floatalloc2(nx, nz);
break;
case 2:
*L2 = floatalloc2(nx, nz);
break;
}
}
void init_arr(int adjwfd, int nx, int nz, float **G, float **L1, float **L2)
{
memset(G[0],0,nx*nz*sizeof(float));
switch (adjwfd){
case 1:
memset(L1[0],0,nx*nz*sizeof(float));
break;
case 2:
memset(L2[0],0,nx*nz*sizeof(float));
break;
}
}
int main(int argc, char *argv[])
{
int adjwfd_P=1, nx=10, nz=10;
float **glob=NULL, **local1=NULL, **local2=NULL;
alloc_arr(adjwfd_P, nx, nz, &glob, &local1, &local2);
init_arr(adjwfd_P, nx, nz, glob, local1, local2);
exit(0);
}
我的问题是为什么我必须使用2D数组的地址,并且在alloc_arr
中仅为分配部分定义3D数组,而在其他功能(例如init_arr
)中,我只能传递原始2D数组插入函数?
My question is why I have to take the address of the 2D array, and in the alloc_arr
define 3D array only for the allocation part, whereas in other functions such as init_arr
, I can just pass the original 2D array into the function?
推荐答案
为什么我必须获取2D数组的地址,并在alloc_arr中仅为分配部分定义3D数组,......
why I have to take the address of the 2D array, and in the alloc_arr define 3D array only for the allocation part,......
在这种情况下:
alloc_arr(adjwfd_P, nx, nz, glob, local1, local2);
您实际上是将最后三个参数作为NULL
传递给alloc_arr()
函数参数G
,L1
和L2
,因为glob
,local1
和local2
用alloc_arr()
You are actually passing the last three argument as NULL
to alloc_arr()
function parameter G
, L1
and L2
as the glob
, local1
and local2
are initialized with NULL
and in alloc_arr()
void alloc_arr(int adjwfd, int nx, int nz, float **G, float **L1, float **L2)
{
G = floatalloc2(nx, nz);
.....
L1 = floatalloc2(nx, nz);
.....
L2 = floatalloc2(nx, nz);
.....
正在将内存分配给G
函数的局部变量的G
,L1
和L2
.在main()
中,返回alloc_arr()
函数后,glob
,local1
和local2
是仍然为空指针.这些空指针传递给init_arr()
,当尝试在init_arr()
中访问它们时,您的程序给出了分段错误.
the memory is getting allocated to G
, L1
and L2
which are local variables of alloc_arr()
function. In the main()
, the glob
, local1
and local2
are still null pointers after alloc_arr()
function returned. These null pointers passed to init_arr()
and when trying to access them in init_arr()
your program is giving segmentation fault.
在这种情况下
alloc_arr(adjwfd_P, nx, nz, &glob, &local1, &local2);
您正在将指针glob
,local1
和local2
的地址作为alloc_arr()
函数参数G
,L1
和L2
的参数传递,这意味着G
,L1
和L2
将分别保存glob
,local1
和local2
指针的地址.
you are passing the address of pointers glob
, local1
and local2
as argument to alloc_arr()
function parameters G
, L1
and L2
, which means G
, L1
and L2
will hold address of glob
, local1
and local2
pointers respectively.
void alloc_arr(int adjwfd, int nx, int nz, float ***G, float ***L1, float ***L2)
{
*G = floatalloc2(nx, nz);
.....
*L1 = floatalloc2(nx, nz);
.....
*L2 = floatalloc2(nx, nz);
.....
取消引用指针G
将得到glob
指针,即*G
为glob
.同样,取消引用*L1
和*L2
将分别给出指针local1
和local2
.因此,当为*G
分配内存时,*L1
和*L2
实际上将为glob
,local1
和local2
指针分配内存.
Dereferencing the pointer G
will give glob
pointer i.e. *G
is glob
. Similarly, dereferencing *L1
and *L2
will give pointer local1
and local2
respectively. So, when allocating memory to *G
, *L1
and *L2
will actually allocate memory to glob
, local1
and local2
pointers.
在其他函数(如init_arr)中,我可以将原始2D数组传递给该函数吗?
whereas in other functions such as init_arr, I can just pass the original 2D array into the function?
看看这个:
alloc_arr(adjwfd_P, nx, nz, &glob, &local1, &local2);
init_arr(adjwfd_P, nx, nz, glob, local1, local2);
alloc_arr()
将为glob
,local1
和local2
分配内存,这意味着从alloc_arr()
返回(假设一切正常)后,指针glob
,local1
和local2
指向有效的内存位置.在init_arr()
中,您只是将这些内存位置作为参数传递给init_arr()
函数参数G
,L1
和L2
.在init_arr()
中,访问G
,L1
和L2
表示它正在访问这些内存位置.
the alloc_arr()
will allocate memory to glob
, local1
and local2
which means after returning from alloc_arr()
(assuming everything works as expected) the pointers glob
, local1
and local2
are pointing to valid memory locations. In the init_arr()
you are just passing those memory locations as argument to the init_arr()
function parameters G
, L1
and L2
. In the init_arr()
, accessing G
, L1
and L2
means it is accessing those memory locations.
其他:
遵循良好的编程习惯,请确保在程序退出之前释放已分配的内存.
Follow good programming practice, make sure to free the allocated memory before program exits.
这篇关于C:在动态分配期间通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!