C ++ init-list:使用非初始化的成员来初始化其他没有给出警告 [英] C++ init-list: using non-initialized members to initialize others gives no warning
问题描述
使用-Wall和-Wextra(+ some others)或-Weverything分别给出了以下代码片段的警告:g ++(4.4和4.6),clang ++(3.2)和coverity都没有。
class B {
char * t2;
char * t;
public:
B():t2(t),t(new char [100]){}
};
我至少希望得到一个关于未初始化(成员)变量使用的小警告。 p>
有没有我缺少的东西?这是一个想要的无警告 - 场景。
编辑:我可以在 href =http://stackoverflow.com/questions/14934911/coverity-and-c-heap-with-new-vs-on-stack-allocation>这个新问题我意识到,
没有理由不在这里发出警告。/ p>
G ++不够聪明,无法诊断构造函数中的非初始化成员,请参见 http:// gcc.gnu.org/PR2972
我有一个进行中的补丁来修复它,我希望在今年的某个时间完成
即使使用我的补丁,我不确定G ++会发出警告,因为 t2
初始化,但它被初始化为不确定的值。对于编译器来跟踪这不是微不足道,但应该是可能的(所以我惊讶,即使Coverity错过它。)运行时工具,如 valgrind
得到它正确。
当我重新访问我的补丁,我会考虑这种情况,看看我是否可以使其警告,而不增加太多的开销(目前我的补丁检查成员没有初始化器将留下数据未初始化,捕获这我需要也检查成员与初始化器,并检查该初始化器是否依赖于另一个尚未初始化的成员,这将需要检查每个成员,这可能对具有大量成员的类的编译速度有影响。)
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:
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
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. 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.
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.)
这篇关于C ++ init-list:使用非初始化的成员来初始化其他没有给出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!