C语言 在 C 中释放 NULL 指针是一种好习惯吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6084218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it good practice to free a NULL pointer in C?
提问by rid
Possible Duplicate:
Does free(ptr) where ptr is NULL corrupt memory?
I'm writing a C function that frees a pointer if it was malloc()ed. The pointer can either be NULL (in the case that an error occured and the code didn't get the chance to allocate anything) or allocated with malloc(). Is it safe to use free(ptr);instead of if (ptr != NULL) free(ptr);?
我正在编写一个 C 函数,如果它被malloc()编辑则释放一个指针。指针可以为 NULL(在发生错误且代码没有机会分配任何内容的情况下)或分配为malloc(). 使用free(ptr);代替安全if (ptr != NULL) free(ptr);吗?
gccdoesn't complain at all, even with -Wall -Wextra -ansi -pedantic, but is it good practice?
gcc根本没有抱怨,即使是-Wall -Wextra -ansi -pedantic,但这是好的做法吗?
回答by orlp
Quoting the C standard, 7.20.3.2/2 from ISO-IEC 9899:
引用ISO-IEC 9899 中的 C 标准 7.20.3.2/2 :
void free(void *ptr);
If
ptris a null pointer, no action occurs.
如果
ptr是空指针,则不执行任何操作。
Don't check for NULL, it only adds more dummy code to read and is thus a bad practice.
不要检查NULL,它只会添加更多要阅读的虚拟代码,因此是一种不好的做法。
However, you must alwayscheck for NULLpointers when using malloc& co. In that case NULLmean that something went wrong, most likely that no memory was available.
但是,在使用& co时必须始终检查NULL指针malloc。在这种情况下,NULL意味着出现问题,很可能是没有可用的内存。
回答by jamesdlin
It is good practice to not bother checking for NULLbefore calling free. Checking just adds unnecessary clutter to your code, and free(NULL)is guaranteed to be safe. From section 7.20.3.2/2 of the C99 standard:
NULL在调用 之前不要打扰检查是一个好习惯free。检查只会给您的代码添加不必要的混乱,并且free(NULL)保证是安全的。来自 C99 标准的第 7.20.3.2/2 节:
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.
free 函数导致 ptr 指向的空间被释放,即,可用于进一步分配。如果 ptr 是空指针,则不会发生任何操作。
As noted in the comments, some people sometimes wonder if checking for NULLis more efficient than making a possibly unnecessary function call. However, this:
正如评论中所指出的,有些人有时想知道检查NULL是否比进行可能不必要的函数调用更有效。然而,这:
- Is a premature micro-optimization.
- Shouldn't matter. Checking for
NULLfirst might be a pessimization. For example, if 99% of the time your pointers aren'tNULL, then there would be an extraNULLcheck 99% of the time to avoid an extra function call 1% of the time.
- 是过早的微优化。
- 应该没关系。
NULL首先检查可能是一种悲观。例如,如果您的指针在 99% 的情况下都不是NULL,那么在NULL99% 的情况下会进行额外的检查,以避免在 1% 的情况下进行额外的函数调用。
回答by Hello71
See http://linux.die.net/man/3/freewhich states:
请参阅http://linux.die.net/man/3/free,其中指出:
If ptr is NULL, no operation is performed.
如果 ptr 为 NULL,则不执行任何操作。
回答by George
In my opinion, no, at least not in your case.
在我看来,不,至少在你的情况下不是。
If you couldn't allocate memory, you should have checked that WAY before the call of free.
如果您无法分配内存,则应该在调用 free 之前检查该方式。

