调用free之前检查NULL [英] checking for NULL before calling free
本文介绍了调用free之前检查NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
许多C code释放指针调用:
Many C code freeing pointers calls:
if (p)
free(p);
但是,为什么?我认为C标准说免费
函数并不做任何事情给出一个NULL指针。那么,为什么另一个明确的检查?
But why? I thought C standard say the free
function doesn't do anything given a NULL pointer. So why another explicit check?
推荐答案
的构造:
free(NULL);
一直确定,在C,回用丹尼斯里奇写原来的UNIX编译器。 pre-标准化,一些贫困的编译器可能无法正确派出,但这些天,没有不能合法地称自己的C语言编译器的编译器。使用它通常会导致更清晰,更易于维护code。
has always been OK in C, back to the original UNIX compiler written by Dennis Ritchie. Pre-standardisation, some poor compilers might not have fielded it correctly, but these days any compiler that does not cannot legitimately call itself a compiler for the C language. Using it typically leads to clearer, more maintainable code.
这篇关于调用free之前检查NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文