以下断言不会失败,尽管根据我的理解它应该会失败。请指正。
#include <stdio.h>
#include <assert.h>
void custom_free(int **temp){
free(*temp);
}
int main(){
int *ptr = malloc(1024);
custom_free(&ptr);
assert(ptr); // doesn't fails ..why?
}
最佳答案
调用free
不会改变指针的值。如果你想 NULL
释放内存,你必须自己做
void custom_free(int **temp){
free(*temp);
*temp = NULL;
}
关于c - 无法从函数中释放指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895778/