简体   繁体   English

具有未初始化指针的类是否具有未定义的行为?

[英]Do classes with uninitialized pointers have undefined behavior?

class someClass
{
public:
    int* ptr2Int;
};

Is this a valid class (yes it compiles)? 这是有效的类吗(是的,可以编译)? Provided one assigns a value to ptr2Int before dereferencing it, is the class guaranteed to work as one would expect? 如果在取消引用之前为ptr2Int分配了一个值,该类是否可以保证正常工作?

An uninitialized pointer inside a class is in no way different from a standalone uninitailized pointer. 类内部的未初始化指针与独立的未完成指针没有什么不同。 As long as you are not using the pointer in any dangerous way, you are fine. 只要您不以任何危险的方式使用指针,就可以了。

Keep in mind though that "dangerous ways" of using an uninitialized pointer include a mere attempt to read its value (no dereference necessary). 请记住,尽管使用未初始化指针的“危险方式”仅包括读取其值的尝试(无需取消引用)。 The implicit compiler-provided copy-constructor and copy-assignment operators present in your class might perform such an attempt if you use these implicit member functions before the pointer gets assigned a valid value. 如果在指针被分配有效值之前使用这些隐式成员函数,则类中存在的由隐式编译器提供的copy-constructor和copy-assignment运算符可能会执行此类尝试。

Actually, if I'm not mistaken, this issue was a matter of some discussion at the level of the standardization committee. 实际上,如果我没记错的话,这个问题是标准化委员会一级的讨论问题。 Are the implicitly generated member functions allowed to trip over trap representations possibly present in the non-initialized members of the class? 是否允许隐式生成的成员函数在类的未初始化成员中可能存在的陷阱表示上跳闸? I don't remember what was the verdict. 我不记得那是什么判决。 (Or maybe I saw that discussion in the context of C99?) (或者也许我在C99的背景下看到了该讨论?)

Yes, it's fine. 是的,很好。 The pointer itself exists, just its value is just unknown, so dereferencing it is unsafe. 指针本身存在,只是其值是未知的,因此取消引用它是不安全的。 Having an uninitialized variable is perfectly fine, and pointers aren't any different 拥有一个未初始化的变量是完全可以的,并且指针没有什么不同

是的,这与带有单个未初始化指针的struct完全相同,并且保证两者都可以正常工作(当然,只要您在使用任何指针之前都设置了指针)。

Until you dereference the pointer it's all good, then it's undefined territory. 在取消引用指针之前,一切都很好,然后便是未定义的区域。

Some compilers will set pointers to default values (like null) depending if you compile in debug or release mode. 某些编译器会将指针设置为默认值(例如null),具体取决于您是在调试还是发布模式下进行编译。 So things could work in one mode and suddenly everything falls apart in another. 因此事情可以在一种模式下工作,突然所有一切都崩溃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有未定义的行为,有两个指针,不同的值引用同一个对象? - Is it undefined behavior to have two pointers with different values referring to the same object? 它是未定义的行为传递引用未初始化的变量? - Is it undefined behavior to pass-by-reference uninitialized variable? 引用未初始化的内存。 未定义的行为? - Reference to uninitialized memory. Undefined behavior? 是否使用未初始化的无符号类型 object 未定义行为? - Is using an uninitialized unsigned type object Undefined Behavior? 在C ++中,访问未初始化的数组是未指定的行为还是未定义的行为? - In C++, is accessing an uninitialized array unspecified behavior or undefined behavior? 为什么常量表达式会排除未定义的行为? - Why do constant expressions have an exclusion for undefined behavior? 为什么在函数体内定义的内置类型的未初始化对象具有未定义的值? - Why do uninitialized objects of built-in type defined inside a function body have undefined value? 减去指针时的 C++ 未定义行为 - C++ Undefined Behavior when subtracting pointers 用未定义的行为代替没有指针的多态性 - Undefined behavior in replacement for polymorphism with out Pointers 指针有大小吗? - Do pointers have a size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM