How to do without observing Visual C ++ standards?

According to this question, Visual C ++ 2005 (as well as 2008/2010) does not zero-initialize the correct data.

Since I have code that requires standard behavior, and which crashes in release mode (rather than debugging), I suspect the problem is coming from here.

Now the problem is that the code base is quite large, and manual class checking is difficult.

Is there a compiler option to trigger a warning about this non-standard MSVC behavior? With / W 4, you get warnings about some non-standard extensions (conversions from rvalues ​​to links, missing keywords typename), but not for this particular problem.

EDIT: I suspect such code is causing problems (pasted from a related question)

include <cassert>

struct B { ~B(); Foo* m; };

int main()
{
   B * b= new B();
   assert ( b->m ==0);
}

in other parts of the code, I have things like

B* b = new B();

and then,

if (b->m) { b->m->bar(); }

and b->mshould be equal to zero by standard, but probably not (except for debug mode). I would like to detect such code (for example, the warning " mused without initialization" or something else)

+3
source share
4 answers

foo::foo () : member () { }? () , . ++ 0 ( ). / . AFAIK . 0 .

+2

asper charles , init ctor.

8.5 An object whose initializer is an empty set of parentheses, i.e., (), shall be
value-initialized. ... Value-initialization for such a class object may be implemented 
by zero-initializing the object and then calling the default constructor

12.1/7 A non-user-provided default constructor for a class ... performs the set of  
initializations of the class that would be performed by a user-written default 
constructor for that class with no ctor-initializer (12.6.2) and an empty
compound-statement.

12.6/4  If a given non-static data member or base class is not named by ... in 
initialiser   list ... the entity is not initialized. 

, ++, dtor, ctor, ctor self-assign ( )

+1

, .

++. , , . , , . script, .

FIrst :

class Foo{
public:

    Foo(int a) : mA(a){}

private:

    int mA, mB;
};

script, : In class Foo :: missing initializer mB

, , :

class Foo{
public:
    Foo(int a) : mA(a), mB(0){}

private:

    int mA, mB;
};

Therefore, my best advice is not to try to get around this problem, to attack it directly. If after this you still have problems, take your nearest static code analyzer and memory analyzer. These are the tools that will help you the best.

+1
source

Are you sure that you (people supposedly quoting the ISO C ++ standard) are actually quoting it? And not to be confused with the text for the updated versions specified in TR, which are actually not standards? Or C ++ 11, which is likely to be soon, but one can hardly expect that MS will meet something else that is not officially a standard.

0
source

Source: https://habr.com/ru/post/1783840/


All Articles