简体   繁体   English

将双指针传递给函数时会发生什么

[英]what happens to a double pointer when it is passed to a function

Im having some trouble understanding how the pass by value mechanism works in c with pointers. 我在理解通过值机制如何在带有指针的c中工作时遇到了一些麻烦。 Here is my contrived example... 这是我做作的例子......


In my main function, I malloc a pointer to an array of int : 在我的main函数中,我malloc指向int数组的指针:

int ** checkMe;
checkMe = malloc(sizeof(int *) * 10);

I understand that this operation sets a side a block of 10 chunks of memory, each block big enough to hold the pointer to an int pointer. 据我所知,这个操作设置了一个包含10块内存的块,每块大到足以容纳指向int指针的指针。 I receive back the pointer at the start of this block of 10 chunks. 我在这块10块的开头收到了指针。

I have another function that takes that double pointer as an argument: 我有另一个函数,将双指针作为参数:

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

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

Whenever I leave the commented section as is, and try to malloc the space for the int in main like this: 每当我按原样离开注释部分,并尝试mallocmainint空间,如下所示:

int ** checkMe;
checkMe = malloc(sizeof(int *) * 10);

for (i = 0; i < 10; i++) {
    checkMe[i] = malloc(sizeof(int));
}

test2dArray(checkMe, 10);

I get memory clobbering whenever I iterate checkMe after the test2dArray call in main . 每当我在maintest2dArray调用之后迭代checkMe时,我都会遇到内存test2dArray

But if I malloc the space for the int in test2dArray instead (by uncommenting the commented line above) and change my call from main to this: 但是,如果我在test2dArrayint替换了malloc (通过取消注释上面的注释行)并将我的调用从main更改为:

int ** checkMe;
checkMe = malloc(sizeof(int *) * 10);

test2dArray(checkMe, 10);

the memory clobbering goes away and I can reference checkMe just fine after the function call. 内存崩溃消失了,我可以在函数调用后很好地引用checkMe


I understand that checkMe is being passed into test2dArray by value. 我知道checkMe是按值传递给test2dArray的。 I think this means that the address that is returned by checkMe = malloc(sizeof(int *) * 10); 我认为这意味着checkMe = malloc(sizeof(int *) * 10);返回的地址checkMe = malloc(sizeof(int *) * 10); is copied into the function. 被复制到函数中。

I don't understand why the int * 's that checkMe stores gets lost if I don't malloc the space from within test2dArray 我不明白为什么如果我不在test2dArray malloc的话,那个checkMe商店的int *会丢失

When you are allocating in main you are not allocating for 10 integers, 当您在main中分配时,您没有分配10个整数,

checkMe[i] = malloc(sizeof(int));

change it to 改为

checkMe[i] = malloc(sizeof(int) * 10);
for (i = 0; i < 10; i++) {
    checkMe[i] = malloc(sizeof(int));
}

You are only allocating memory for 1 int in each loop iteration. 您只在每个循环迭代中为1 int分配内存。 So you have an array of 10 pointers, each pointing to sizeof(int) bytes of memory. 所以你有一个包含10个指针的数组,每个指针都指向sizeof(int)字节的内存。

test2dArray(checkMe, 10);

only works for arrays of 10 pointers pointing to at least 10*sizeof(int) memory. 仅适用于指向至少10*sizeof(int)内存的10个指针的数组。 You should change the line above to checkMe[i] = malloc(sizeof(int)*10) ; 您应该将上面的行更改为checkMe[i] = malloc(sizeof(int)*10) ;

Your bug is the difference between this: 你的错误是这之间的区别:

checkMe[i] = malloc(sizeof(int));

and this: 和这个:

arr[i] = malloc(sizeof(int) * size); // size = 10

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

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