Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 指针问题_C_Pointers_Stack Unwinding - Fatal编程技术网

C 指针问题

C 指针问题,c,pointers,stack-unwinding,C,Pointers,Stack Unwinding,好的,我看了两层函数fun1调用func2调用func3。我基本上使用int*ptr向下传递一个指针,在调用堆栈的最低“级别”,我还有另一个为int数组动态分配内存的函数。在顶层(func1层),我总是为传递的指针返回null。我追溯到func3,分配的内存中充满了值,但当调用堆栈展开func3->func2时,指针突然消失(0x0000_0000)?我不明白在func3级别,我基本上说ptr=allocate\u ptr\u array,但从这个返回值开始,它变为NULL!即使我没有释放记忆,

好的,我看了两层函数fun1调用func2调用func3。我基本上使用int*ptr向下传递一个指针,在调用堆栈的最低“级别”,我还有另一个为int数组动态分配内存的函数。在顶层(func1层),我总是为传递的指针返回null。我追溯到func3,分配的内存中充满了值,但当调用堆栈展开func3->func2时,指针突然消失(0x0000_0000)?我不明白在func3级别,我基本上说ptr=allocate\u ptr\u array,但从这个返回值开始,它变为NULL!即使我没有释放记忆,到底发生了什么?我知道我的问题令人困惑。我在调试器中看到了这种情况,尽管指针基本上是按值传递的。您需要将指针传递给指针(int**p),以便在外部函数中重新分配内存

function1(int *p)
{
 p = //allocate memory using malloc
}

function2(int **p)
{
 *p = //allocate memory using malloc
}

function3()
{
 int *p;
 function1(p); 
// in this case pointer is passed by value. 
//The memory allocated will not be available in p after the function call function1.

int **p;
function2(&p); 
//in this case pointer to pointer p has been passed.
// P will have the memory allocated even after 
//the function call function1
}
}

用一些代码说明aJ(完全正确)的答案:

void func1(void)
{
    int *int_array;

    func2(&int_array);

    /* Some stuff using int_array[0] etc */

    /* ... */

    free(int_array);
}

void func2(int **a)
{
     /* ... stuff ... */

     func3(a);

     /* .... stuff ... */
}

void func3(int **a)
{
    (*a) = malloc(N * sizeof **a);
}

这是一个很好的例子,供其他人将来参考。这在实施之后是有意义的,多亏了这些家伙

#include <memory.h>
#include <stdlib.h>
#include <stdio.h>

void func3(int **ptr)
{
    int i;
    (*ptr) = (int *)malloc(25*sizeof(int));

    for (i=0; i < 25; i++) (**ptr) = i;
    printf("func3: %d\n",ptr);
}

void func2(int **ptr)
{
    func3(ptr);
    printf("func2: %d\n", ptr);
}
void func1(void)
{
    int *ptr;
    printf("ptr before: %d\n", ptr);
    func2(&ptr);
    printf("ptr after: %d\n", ptr);
}

void func4(int **ptr)
{
    static int stuff[25];
    printf("stuff: %d\n",stuff);
    *ptr = stuff;
}

int main(void)
{
    int *painter;
    func1();
    func4(&painter);
    printf("painter: %d\n", painter);
    return 0;
}
#包括
#包括
#包括
无效函数3(整数**ptr)
{
int i;
(*ptr)=(int*)malloc(25*sizeof(int));
对于(i=0;i<25;i++)(**ptr)=i;
printf(“func3:%d\n”,ptr);
}
无效函数2(整数**ptr)
{
职能3(ptr);
printf(“func2:%d\n”,ptr);
}
无效功能1(无效)
{
int*ptr;
printf(“ptr之前:%d\n”,ptr);
职能2(&ptr);
printf(“ptr后:%d\n”,ptr);
}
无效函数4(整数**ptr)
{
静态int-stuff[25];
printf(“内容:%d\n”,内容);
*ptr=材料;
}
内部主(空)
{
国际*画家;
func1();
职能4(油漆工和油漆工);
printf(“画师:%d\n”,画师);
返回0;
}

你的意思是我不能拥有这个?func1(void){int-ptr;func2(int-ptr);//仅显示类型样式)}func2(int*ptr){func3(int*ptr);}func3(int*ptr){malloced_数组上做一些工作;ptr=&数组[0]}您的意思是在调用解除时其中一个会被删除?在func2中,我注意到ptr变为NULL。我想你可以把它直接传给电话吗?它没有被删除。func3中分配的内存地址在外部函数中不可用。这基本上是内存泄漏。你可以再做一件事。使func3返回int*而不是作为参数。在外部函数中添加int*p=func3();这将确保你得到内部函数中分配的内存指针。我记得在我之前遇到过这个问题,很难理解为什么会发生这种情况。好问题,可以用一个不太一般的标题。所以基本上我需要一个指针,指向一个一直向下的指针,因为它是在底层分配的?这对我来说是一种新的东西,通常我在func1中分配,然后将“数组”和大小一起传递。对不起,指针总是扔我!是的,因为您希望底层更新存在于顶层的变量的值,所以您需要向下传递一个指向该变量的指针。碰巧的是,本例中的变量已经是指针了,所以您最终会传递一个指针到一个指针。另一种方法是让低级函数返回新的指针值,并在顶层显式地将返回值分配给int_数组。这就是标准库函数的工作方式。唯一的问题是我实际上有两个数组要返回:(所以我必须通过params来完成。谢谢!如果数组彼此相关,那么你可以将两个指针包装在一个结构中并返回它。