我写这样的代码:ptr = free(ptr),NULL安全吗?
#include <stdlib.h>
int main(void)
{
void *kilobyte;
kilobyte = malloc(1024);
kilobyte = NULL, free(kilobyte);
return 0;
}
的对称性,这是很好的。但我从来没有见过任何人使用之前这个成语,所以,我不知道这实际上可能是不可移植/不安全的,尽管这Wikipedia报价:
In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).
编辑:混合起来的顺序。现在它编译在gcc
没有任何警告。
'free'没有返回值,所以该行不良作风。代码甚至没有更少的输入,使用标准的代码。 – Olaf
AFAIK,逗号运算符丢弃'free'值,这是'void',并使用'NULL'代替。如果你的风格需求在释放它们之后始终将指针指向NULL,那么它**少输入。 –
丢弃没有价值!你有没有试图在问之前编译这个?你有没有在标准中找到合法或非法的证据?请引用该部分。 (哦,我用gcc尝试过)。 – Olaf