简体   繁体   English

C ++ init-list:使用非初始化成员初始化其他成员不会发出警告

[英]C++ init-list: using non-initialized members to initialize others gives no warning

Neither g++ (4.4 and 4.6) nor clang++ (3.2) nor coverity, with -Wall and -Wextra (+ some others) or -Weverything respectively gives me a warning for the following code snippet: 既没有g ++(4.4和4.6)也没有clang ++(3.2),也没有覆盖,-Wall和-Wextra(+其他一些)或-Weverything分别给出了以下代码片段的警告:

class B {
    char *t2;
    char *t;

public:
    B() : t2(t), t(new char[100]) {}
};

I would at least expect a small warning about the usage of uninitialized (member-) variables. 我至少会期待一个关于未初始化(成员)变量使用的小警告。

Is there something I'm missing? 有什么我想念的吗? Is this a wanted "no-warning"-scenario. 这是一个想要的“无警告” - 场景。 I have (now had) at least one bug in my software which was hard to find. 我(现在已经)我的软件中至少有一个很难找到的错误。

EDIT : As can be read in this new question I realized that coverity warns about this problem in some cases. 编辑 :从这个新问题可以看出,我意识到在某些情况下, 封面警告这个问题。

There is no good reason not to issue a warning here. 没有充分的理由不在这里发出警告。

G++ isn't smart enough to diagnose unintialized members in constructors, see http://gcc.gnu.org/PR2972 G ++不够智能,无法诊断构造函数中的非实体成员,请参阅http://gcc.gnu.org/PR2972

I have a work-in-progress patch to fix it which I hope to finish "some time this year" 我有一个正在进行中的补丁修复它,我希望“今年某个时候”完成

Even with my patch I'm not sure G++ would warn, because t2 is initialized, but it's initialized to an indeterminate value. 即使使用我的补丁,我也不确定G ++是否会发出警告,因为t2 初始化,但它已被初始化为不确定的值。 For the compiler to track that is not trivial, but should be possible (so I'm surprised even Coverity misses it.) Run-time tools such as valgrind get it right though. 对于编译器来说,跟踪这不是一件容易的事情,但应该是可能的(所以即使Coverity错过了我也很惊讶。)像valgrind这样的运行时工具可以解决它。

When I revisit my patch I'll consider this case and see whether I can make it warn, without adding too much overhead (currently my patch checks whether members without an initializer would leave data uninitialized, to catch this I would need to also check members with an initializer and check whether that initializer relies on another member which isn't yet initialized, which would need to be checked for every member, which might have an effect on compilation speed for classes with lots of members.) 当我重新访问我的补丁时,我会考虑这种情况,看看我是否可以发出警告,而不会增加太多开销(目前我的补丁检查没有初始化程序的成员是否会保留数据未初始化,要抓住这个我还需要检查成员使用初始化程序并检查初始化程序是否依赖于另一个尚未初始化的成员,这需要为每个成员检查,这可能会对具有大量成员的类的编译速度产生影响。)

The C++ standard says that using uninitialized variables leads to undefined behaviour. C ++标准说使用未初始化的变量会导致未定义的行为。 It does not mandate that the compiler issue a diagnostic about it. 它并不要求编译器对其发出诊断。 So getting a warning or not is a QOI (Quality of Implementation) thing. 所以得到警告与否是QOI(实施质量)的事情。

暂无
暂无

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

相关问题 C ++-局部未初始化变量的值 - C++ - Value of local non-initialized variables 使用指向初始化器列表中未初始化成员的指针 - Using pointer to non-initialized member in initializer list 与普通cout的行为相比,为什么c ++中的for循环以未初始化的方式访问内存位置? - Why does a for loop in c++ access a memory location in non-initialized compared to the behavior of a normal cout? 为什么零初始化不是c ++中非初始化变量的默认值? 是否有强制它的编译器选项? - Why is zero-initialization not the default for non-initialized variables in c++? Is there a compiler option to force it? C ++使用&从向量获取结构,并返回指向未初始化结构的指针 - C++ getting struct from vector with & returns pointer to non-initialized struct 使用支撑的init-list在向量中插入新元素 - Inserting a new element in vector using a braced init-list 为什么在 C++ 中未初始化数组中有随机数,而在半初始化数组的未初始化成员中没有? - Why there are Random numbers in non initialized array but not in non initialized members of half initialized array in C++? 在C ++中使用指针初始化结构成员 - initialize structure members using pointer in c++ C++:如何使用初始化列表初始化内部结构的成员? - C++: How do you initialize an internal struct's members using an initialization list? 在 C++ 中初始化 class 的成员 - Initialize members of a class in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM