开发者社区> 问答> 正文

C的free的参数是空指针的问题

这样一段代码

#include<stdio.h>
int main(){
    int *p;
    p = NULL;
    free(p);
    printf("test\n");
    return 0;
}

最后可以使用gcc编译成功并且输出结果是:

test
为什么free一个空指针没有报错,反而可以顺利运行到输出语句?

展开
收起
a123456678 2016-06-08 19:43:34 3981 0
1 条回答
写回答
取消 提交回答
  • The free() function frees the memory space pointed to by ptr, which
    must have been returned by a previous call to malloc(), calloc() or
    realloc(). Otherwise, or if free(ptr) has already been called before,
    undefined behavior occurs. If ptr is NULL, no operation is performed.
    2019-07-17 19:32:36
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载