首页 > C的free的参数是空指针的问题

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

这样一段代码

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

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

test

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


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.


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

【热门文章】
【热门文章】