是否在释放不确定的行为未初始化的指针的结果? [英] Does freeing an uninitialized pointer result in undefined behavior?

查看:139
本文介绍了是否在释放不确定的行为未初始化的指针的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有没有错误初始化,指针,尽量释放它,这是将导致不确定的行为?

我爱:

  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 by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

这篇关于是否在释放不确定的行为未初始化的指针的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆