指针问题

发布于 2024-07-30 08:10:12 字数 340 浏览 2 评论 0原文

好吧,我经历了 2 层函数 fun1 调用 func2 调用 func3 。 我基本上使用 int *ptr 一路向下传递指针,在调用堆栈的最低“级别”,我还有另一个为 int 数组动态分配内存的函数。 在顶层(func1 级别)我总是为传递的指针返回 null。 我已经追踪到 func3 并且分配的内存正在填充值,但是随着调用堆栈展开 func3 -> func2 指针突然消失(0x0000_0000)? 我在 func3 级别不明白,我基本上说 ptr = allocate_ptr_array,但从该返回值开始,它变为 NULL! 即使我没有释放内存,到底发生了什么? 我知道我的问题很令人困惑。 我已经在调试器中看到了这种情况的发生

Okay I go through 2 layers of functions fun1 calls func2 calls func3 . I pass a pointer all the way down using basically int *ptr, at the lowest "level" of the call stack I also have another function that dynamically allocates memory for an int array. At the top level (func1 level) I always get null back for the passed pointer. I have traced down to func3 and the allocated memory is being filled with values, but as the call stack unwinds func3 -> func2 suddenly the pointer just goes away (0x0000_0000)? I don't understand at func3 level I basically say ptr = allocate_ptr_array, but from that return it goes to NULL! Even though I didn't free the memory, what in the world is going on? I know my question is confusing. I have watched this happen in the debugger though

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋竹姑娘 2024-08-06 08:10:12

指针基本上是按值传递的。 您需要将指针传递给指针(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
}

}

The pointer is basically passed by value. You need to pass pointer to pointer (int **p) to get the memory allocated back in outer function.

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
}

}

电影里的梦 2024-08-06 08:10:12

用一些代码来阐明 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);
}

To illuminate aJ's (completely correct) answer with some code:

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);
}
嗳卜坏 2024-08-06 08:10:12

这是一个很好的例子,供其他人将来参考。 实施后这是有意义的,感谢这些人。

#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;
}

Here is a good example for future reference bye other people. It makes sense after implementation and thanks to these guys.

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文