未初始化和不确定之间的差异 [英] Difference between uninitialized and indeterminate

查看:130
本文介绍了未初始化和不确定之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读类型名称后的括号与新的有所不同



Michael Burr
谈论未初始化 indeterminate 值。想要知道之间的区别。



我的理解是,未初始化意味着,编译器将分配内存不尝试初始化对象。 indeterminate - >访问此对象可能会导致未定义的行为。
如果我错了,请更正我。



也想知道默认初始化之间的区别值初始化

解决方案



未初始化表示内存已保留,有意义的数据。一个简单的例子:

  int * ptr; 

这是保留一块内存来包含指针,但由于没有int ,该指针的值不会改变,并且包含一个随机的垃圾地址到无意义的位置。



另一个示例:

  class MyClass 
{
int a;
int b;

MyClass():
a(b)// Oops
b(5)
{}
}

int b / code>,再次包含随机垃圾。






code> state是当对象被初始化,但是(可能因为它不打算再被使用)你不知道里面有什么。



它的行为是不可预测的,但不是无效的(除非,当然,文档说它也无效)。例如:

  std :: string a(text); 
std :: string b(std :: move(a));

只是一个空字符串。访问它不会导致未定义的行为,它将像任何其他空字符串一样工作。除非你不知道它是空的。



对于所有你知道,它可能已经替换了它的char数组引用一些静态const字符数组这是一个不确定的对象,为什么你访问我离开我独自。。或者一些复杂的系列看似随机的字符,是内部优化的结果。你不能知道它是什么,不应该使用它。但是当你这样做,它不会给你未定义的行为。对未定义的数据只是无用的行为。






默认初始化值初始化不是您的主要问题的一部分,您应该问一个单独的问题(假设你不能在这个网站上的答案)。 / p>

I was reading Do the parentheses after the type name make a difference with new

There Michael Burr talks about uninitialized and indeterminate values. Want to know the difference between the same.

My understanding is that, uninitialized means, compiler will allocate memory not try to initialize the object. indeterminate-> accessing this object might cause undefined behavior. Please correct me if I am wrong.

Also want to know the difference between default-initialization and value-initialization.

解决方案

I think your interpretation is close, but not exactly right.

uninitialized means the memory is reserved, but not filled with meaningful data. An easy example:

int* ptr;

All this does is reserve a piece of memory to contain the pointer, but since there is no int yet, the value of that pointer is not altered and contains a random garbage address to a meaningless location.

Another example:

class MyClass
{
    int a;
    int b;

    MyClass() :
        a(b) //Oops
        b(5)
    { }
}

int b is not initialized yet when using it for a and, again, contains random garbage.


An indeterminate state is when the object has been initialized, but (probably because it is not intended to be used anymore) you don't know what's inside it.

Its behaviour is unpredictable, but not invalid (unless, of course, the documentation says it's invalid as well). For instance:

std::string a("text");
std::string b(std::move(a));

string a is now probably just an empty string. Accessing it will not cause undefined behavior, it will behave like any other empty string. Except you don't know for sure that it's empty.

For all you know, it might have replaced its char array with a reference to some static const char array saying "this is an indeterminate object why are you accessing me leave me alone.". Or some complicated series of seemingly random characters that is the result of internal optimizations. You can't know what is in it, and shouldn't use it. But when you do, it won't give you undefined behavior. Just useless behavior, on undefined data.


default-initialization and value-initialization are not part of your main question, you should ask for those in a separate question (assuming you can't find the answer on this site already).

这篇关于未初始化和不确定之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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