为什么我的指针释放后不为空?

发布于 2024-12-07 12:23:36 字数 295 浏览 4 评论 0原文

void getFree(void *ptr)
{
    if(ptr != NULL)
    {
        free(ptr);
        ptr = NULL;
    }
    return;
}
int main()
{
char *a;
a=malloc(10);
getFree(a);
if(a==NULL)
    printf("it is null");
else
    printf("not null");
}

为什么这个程序的输出不是NULL?

void getFree(void *ptr)
{
    if(ptr != NULL)
    {
        free(ptr);
        ptr = NULL;
    }
    return;
}
int main()
{
char *a;
a=malloc(10);
getFree(a);
if(a==NULL)
    printf("it is null");
else
    printf("not null");
}

Why is the output of this program not NULL?

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

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

发布评论

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

评论(6

_失温 2024-12-14 12:23:36

因为指针是按值复制到您的函数的。您正在将 NULL 分配给变量的本地副本 (ptr)。这不会将其分配给原始副本。

内存仍将被释放,因此您无法再安全地访问它,但您的原始指针将不再是NULL

这与将 int 传递给函数相同。您不会期望原始 int 被该函数编辑,除非您将指针传递给它。

void setInt(int someValue) {
    someValue = 5;
}

int main() {
    int someOtherValue = 7;
    setInt(someOtherValue);
    printf("%i\n", someOtherValue); // You'd expect this to print 7, not 5...
    return 0;
}

如果你想将原始指针清空,你必须传递一个指针到指针:

void getFree(void** ptr) {
    /* Note we are dereferencing the outer pointer,
    so we're directly editing the original pointer */

    if (*ptr != NULL) {
        /* The C standard guarantees that free() safely handles NULL,
           but I'm leaving the NULL check to make the example more clear.
           Remove the "if" check above, in your own code */
        free(*ptr);
        *ptr = NULL;
    }

    return;
}

int main() {
    char *a;
    a = malloc(10);

    getFree(&a); /* Pass a pointer-to-pointer */

    if (a == NULL) {
        printf("it is null");
    } else {
        printf("not null");
    }

    return 0;
}

Because the pointer is copied by value to your function. You are assigning NULL to the local copy of the variable (ptr). This does not assign it to the original copy.

The memory will still be freed, so you can no longer safely access it, but your original pointer will not be NULL.

This the same as if you were passing an int to a function instead. You wouldn't expect the original int to be edited by that function, unless you were passing a pointer to it.

void setInt(int someValue) {
    someValue = 5;
}

int main() {
    int someOtherValue = 7;
    setInt(someOtherValue);
    printf("%i\n", someOtherValue); // You'd expect this to print 7, not 5...
    return 0;
}

If you want to null the original pointer, you'll have to pass a pointer-to-pointer:

void getFree(void** ptr) {
    /* Note we are dereferencing the outer pointer,
    so we're directly editing the original pointer */

    if (*ptr != NULL) {
        /* The C standard guarantees that free() safely handles NULL,
           but I'm leaving the NULL check to make the example more clear.
           Remove the "if" check above, in your own code */
        free(*ptr);
        *ptr = NULL;
    }

    return;
}

int main() {
    char *a;
    a = malloc(10);

    getFree(&a); /* Pass a pointer-to-pointer */

    if (a == NULL) {
        printf("it is null");
    } else {
        printf("not null");
    }

    return 0;
}
亢潮 2024-12-14 12:23:36

因为 getFree() 函数获取指针的副本。 ptrc 都是指针,但它们是不同的变量。这与该函数输出“6”的原因相同:

void Magic(int x)
{
    x = 1;
}

void main()
{
    int a = 6;
    Magic(a);
    printf("%d", a);
}

Because the getFree() function takes a copy of the pointer. ptr and c are both pointers, but they are different variables. It's the same reason why this function will output "6":

void Magic(int x)
{
    x = 1;
}

void main()
{
    int a = 6;
    Magic(a);
    printf("%d", a);
}
不语却知心 2024-12-14 12:23:36

您按值传递指针 a,因此它不会被函数修改。它只是函数内修改的指针的副本,原始变量值不受影响。

更新:

如果您想通过用一行代码替换释放+清空变量来让您的生活更轻松,您需要一个宏:

#define MYFREE(x) free(x); x = NULL;

或一个带有指向指针参数的函数:

void myfree(void** pp) { free(*pp); *pp = NULL; }

You are passing pointer a by value, so it is not modified by function. It's only a copy of pointer modified within function, the original variable value is not affected.

Update:

If you wanted to make your life easier by replacing freeing + nulling a variable with a single line of code, you need either a macro:

#define MYFREE(x) free(x); x = NULL;

or a function with pointer to pointer argument:

void myfree(void** pp) { free(*pp); *pp = NULL; }
君勿笑 2024-12-14 12:23:36

指针以整数形式存储在内存中的某个位置。

当您执行 a = malloc(10); 时,a 具有一些值,例如 0x1。

当您调用 getFree(a); 时,该函数会将 a 复制到 void *ptr 中。

现在a=0x1ptr=0x1

当你执行ptr=NULL时,只有ptr更改为NULL,但a仍然是0x1..

Pointers are stored as integers somewhere in memory.

When you do a = malloc(10);, a has some value, say 0x1.

When you call getFree(a);, the function copies a into void *ptr.

Now a=0x1 and ptr=0x1.

When you do ptr=NULL, only ptr is changed to NULL, but a is still 0x1..

坦然微笑 2024-12-14 12:23:36

您正在按值传递指针..(默认情况下C按值传递参数)这意味着您仅更新副本..而不是真实位置..因为您可能需要在C中使用指向指针的指针

void getFree(void **ptr)
{

    if(*ptr != NULL)
    {
        free(*ptr);
        *ptr = NULL;
    }

    return;
}

You are passing the pointer By value.. (By default C passes the argument by value) which means you are updating the copy only ..not the real location..for that you might need to use pointer to pointer in C

void getFree(void **ptr)
{

    if(*ptr != NULL)
    {
        free(*ptr);
        *ptr = NULL;
    }

    return;
}
紧拥背影 2024-12-14 12:23:36

这个问题已经得到解答,但如果有帮助,我可以用图形方式解释它。

您正在执行此操作 -->指针按值复制到您的函数,因此它指向数组

但您想要这个 -->指向原始指针

正如 Merlyn Morgan-Graham 已经说过的,解决它的方法是添加运算符 * 和 &。

The question has already been answered but if it helps, I can explain it graphically.

You are doing this --> the pointer is copied by value to your function so it points to the array

but instead you want this -->point to the original pointer

As Merlyn Morgan-Graham already said, the way to solve it is to add operators * and &.

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