简体   繁体   English

如何通过函数将指针传递给c中未声明的可修改1 / 2D数组并保持其值?

[英]How do I pass pointers to undeclared modifiable 1/2D arrays in c through a function and keep their values?

I am writing a function in C that needs to modify multiple variables as it progresses, specifically the last 3 args. 我在C中编写一个函数,需要在进展时修改多个变量,特别是最后3个参数。 The function declaration is shown below: 函数声明如下所示:

void Sample_v2(double cyl_radius, double thetaOx, double binsize, double ** newRx, double * binCenterThetas, double ** binCenters) 

And my variables are as follows: 我的变量如下:

int main() {
    // initialize function outputs
    double ** GPU_binCenters;
    double ** GPU_Rx;
    double * GPU_binCenterThetas;
    // set testing values
    double cyl_rad = 0.3;
    double theta = 1.5708;
    double binsiz = 0.0025;
    int n;

    Sample_v2(cyl_rad, theta, binsiz, GPU_Rx, GPU_binCenterThetas, GPU_binCenters);

    fprintf(stdout,"GPU_binCenters\n");
    for (n = 0; n < 188; n++) {
        fprintf(stdout, "%G\n", GPU_binCenterThetas[n]);
    }
}

I give my arrays values with the following code: 我用以下代码给出了我的数组值:

newRx = (double**) calloc((int)ceil(howmany) * 2, sizeof(double*));
for (ii = 0; ii < (int)ceil(howmany) * 2; ii++) {
    newRx[ii] = (double*) calloc(2, sizeof(double));
}

or 要么

binCenterThetas = (double*) calloc((int)ceil(howmany)*2+1, sizeof(double));

And get a segfault when I try to run this on the fprintf statement in the for loop, the first time it runs. 当我尝试在for循环中的fprintf语句上运行它时,第一次运行时会出现段错误。 Using gdb I am not able to manually read values as well, but I can see the pointer address. 使用gdb我也无法手动读取值,但我可以看到指针地址。 I think the pointer is being copied when I pass it, but I tried implementing another solution passing my variables that need to be modified by reference and had issues with that as well. 我认为当我传递它时指针被复制,但我尝试实现另一个传递我需要通过引用修改的变量的解决方案,并且也存在问题。 The code for that is below: 代码如下:

Sample_v2(cyl_rad, theta, binsiz, &GPU_Rx, &GPU_binCenterThetas, &GPU_binCenters);

    ...........

void Sample_v2(double cyl_radius, double thetaOx, double binsize, double *** newRx, double ** binCenterThetas, double *** binCenters) {

    ...........

*newRx = (double**) malloc((int)ceil(howmany) * 2 * sizeof(double*));
for (ii = 0; ii < (int)ceil(howmany) * 2; ii++) {
    *newRx[ii] = (double*) malloc(2 * sizeof(double));
}

That code segfaults when allocating the memory within the for loop. 在for循环中分配内存时,该代码会出现段错误。 Any help on how I can actually get my array to either be passed out or initialize without segfaulting. 有关如何实际获取我的数组的任何帮助要么被传递出去,要么在没有segfaulting的情况下进行初始化。 Note that I am not using a return statement as I have 3 arrays that are modified within the function. 请注意,我没有使用return语句,因为我在函数中修改了3个数组。

When you call 你打电话时

Sample_v2(cyl_rad, theta, binsiz, GPU_Rx, GPU_binCenterThetas, GPU_binCenters);

the arguments received by Sample_v2 are copies of the pointers in main , and any changes done in Sample_v2 to those pointers don't influence the pointers in main , so when you enter the fprintf loop, GPU_binCenterThetas is still an uninitialised pointer, and accessing GPU_binCenterThetas[n] is undefined behaviour and likely to cause a segfault. 所收到的参数Sample_v2都在指针的副本main ,在做任何更改Sample_v2那些指针不影响指针main ,所以当你进入fprintf环, GPU_binCenterThetas仍然是一个未初始化的指针,并访问GPU_binCenterThetas[n]是未定义的行为,可能会导致段错误。

The correct way to handle that is indeed to add a layer of indirection and pass the addresses of these pointers (or to define a struct containing three pointers of appropriate types and return that; if there was only one pointer to modify, returning the pointer would be my preferred way - if the pointers are returned, they need not be passed as arguments). 处理它的正确方法确实是添加一个间接层并传递这些指针的地址(或者定义一个包含三个适当类型指针的struct并返回它;如果只有一个指针要修改,则返回指针会是我的首选方式 - 如果返回指针,则不需要将它们作为参数传递)。

The problem with your attempt 你尝试的问题

Sample_v2(cyl_rad, theta, binsiz, &GPU_Rx, &GPU_binCenterThetas, &GPU_binCenters);

    ...........

void Sample_v2(double cyl_radius, double thetaOx, double binsize, double *** newRx, double ** binCenterThetas, double *** binCenters) {

    ...........

*newRx = (double**) malloc((int)ceil(howmany) * 2 * sizeof(double*));
for (ii = 0; ii < (int)ceil(howmany) * 2; ii++) {
    *newRx[ii] = (double*) malloc(2 * sizeof(double));
}

is that you have gotten the precedence wrong. 是你的优先权是错的。

*newRx[ii]

is

*(newRx[ii])

but newRx[ii] is only a valid pointer for ii == 0 . 但是newRx[ii]只是ii == 0的有效指针。 What you meant was 你的意思是

(*newRx)[ii]

(and similar for the others). (和其他人类似)。

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

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