为什么我的指针释放后不为空?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为指针是按值复制到您的函数的。您正在将
NULL
分配给变量的本地副本 (ptr
)。这不会将其分配给原始副本。内存仍将被释放,因此您无法再安全地访问它,但您的原始指针将不再是
NULL
。这与将
int
传递给函数相同。您不会期望原始int
被该函数编辑,除非您将指针传递给它。如果你想将原始指针清空,你必须传递一个指针到指针:
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 originalint
to be edited by that function, unless you were passing a pointer to it.If you want to null the original pointer, you'll have to pass a pointer-to-pointer:
因为
getFree()
函数获取指针的副本。ptr
和c
都是指针,但它们是不同的变量。这与该函数输出“6”的原因相同:Because the
getFree()
function takes a copy of the pointer.ptr
andc
are both pointers, but they are different variables. It's the same reason why this function will output "6":您按值传递指针
a
,因此它不会被函数修改。它只是函数内修改的指针的副本,原始变量值不受影响。更新:
如果您想通过用一行代码替换释放+清空变量来让您的生活更轻松,您需要一个宏:
或一个带有指向指针参数的函数:
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:
or a function with pointer to pointer argument:
指针以整数形式存储在内存中的某个位置。
当您执行
a = malloc(10);
时,a
具有一些值,例如 0x1。当您调用
getFree(a);
时,该函数会将a
复制到void *ptr
中。现在
a=0x1
和ptr=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 copiesa
intovoid *ptr
.Now
a=0x1
andptr=0x1
.When you do
ptr=NULL
, onlyptr
is changed to NULL, buta
is still 0x1..您正在按值传递指针..(默认情况下C按值传递参数)这意味着您仅更新副本..而不是真实位置..因为您可能需要在C中使用指向指针的指针
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
这个问题已经得到解答,但如果有帮助,我可以用图形方式解释它。
您正在执行此操作 -->指针按值复制到您的函数,因此它指向数组
但您想要这个 -->指向原始指针
正如 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 &.