c - 无法从函数中释放指针

标签 c pointers memory

以下断言不会失败,尽管根据我的理解它应该会失败。请指正。

#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/

相关文章:

c - malloc中sizeof的使用

c - 在 C 中使用 Realloc

java - [JAVA]如何创建排名系统并将其保存到.txt 文件?

c - 使用fgets()和strtok()读取文本文件以分隔行中的字符串,从而产生不必要的行为

Swift 3 - UIImageWriteToSavedPhotosAlbum 内存问题

c - C 中的段错误

javascript - WebAssembly 链接错误 : function import requires a callable

c - 如何从C中的字符串中提取特定字符(http响应)

c - 从c中的标准输入读取直到EOF

pointers - 取消引用空指针会导致内核崩溃