C:在动态分配期间通过引用传递 [英] C: Passing by reference during dynamic allocaion

查看:98
本文介绍了C:在动态分配期间通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下分配和初始化数组(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()函数参数GL1L2,因为globlocal1local2alloc_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函数的局部变量GL1L2.在main()中,返回alloc_arr()函数后,globlocal1local2仍然为空指针.这些空指针传递给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);

您正在将指针globlocal1local2的地址作为alloc_arr()函数参数GL1L2的参数传递,这意味着GL1L2将分别保存globlocal1local2指针的地址.

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指针,即*Gglob.同样,取消引用*L1*L2将分别给出指针local1local2.因此,当为*G分配内存时,*L1*L2实际上将为globlocal1local2指针分配内存.

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()将为globlocal1local2分配内存,这意味着从alloc_arr()返回(假设一切正常)后,指针globlocal1local2指向有效的内存位置.在init_arr()中,您只是将这些内存位置作为参数传递给init_arr()函数参数GL1L2.在init_arr()中,访问GL1L2表示它正在访问这些内存位置.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆