开发者社区 问答 正文

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 4215 分享 版权
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
    赞同 展开评论
问答分类:
问答标签:
问答地址: