1

这样一段代码

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

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

test

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

xumenger 4.6k
2015-09-02 提问
2 个回答
1

已采纳

http://linux.die.net/man/3/malloc

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.

0

都知道空指针不需要free,编译器这点智能还是有的

撰写答案

推广链接