是否在释放不确定的行为未初始化的指针的结果? [英] Does freeing an uninitialized pointer result in undefined behavior?
问题描述
如果您有没有错误初始化,指针,尽量释放它,这是将导致不确定的行为?
我爱:
INT主要(无效){ 字符*串;
免费(字符串); 返回0;
}
是否在释放不确定的行为未初始化的指针的结果?
块引用>是
然而,腾出一个空指针是明确的。
从C99标准:
的
免费
功能使空间指向PTR
被释放,也就是进一步提供分配。如果PTR
是一个空指针时,没有采取行动。否则,如果该参数不匹配由释放calloc
,早期的malloc返回一个指针
或
的realloc
函数,或者如果空间已释放通过调用免费
或realloc的
,在行为是未定义
块引用>If you have a pointer that is not initialized and, by mistake, try to free it, is this going to result in undefined behavior?
Like:
int main(void){ char *string; free(string); return 0; }
解决方案Does freeing an uninitialized pointer result in undefined behavior?
Yes.
However, freeing a null pointer is well-defined.
From the C99 standard:
The
free
function causes the space pointed to byptr
to be deallocated, that is, made available for further allocation. Ifptr
is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by thecalloc
,malloc
, orrealloc
function, or if the space has been deallocated by a call tofree
orrealloc
, the behavior is undefined.
这篇关于是否在释放不确定的行为未初始化的指针的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!