Export (0) Print
Expand All
Expand Minimize

Compiler Warning (level 2) C4345

behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized

This warning reports a behavior change from the Visual C++ compiler that shipped in Visual Studio .NET when initializing a POD object with (); the compiler will default-initialize the object.

See Summary of Compile-Time Breaking Changes for more information.

The following sample generates C4345:

// C4345.cpp
// compile with: /W2
#include <stdio.h>

struct S* gpS;

struct S
{
   // this class has no user-defined default ctor
   void *operator new (size_t size, void*p, int i)
   {
      ((S*)p)->i = i;   // ordinarily, should not initialize
                        // memory contents inside placement new
      return p;
   }
   int i;
};

int main()
{
   S s;
   // Visual C++ .NET 2003 will default-initialize pS->i
   // by assigning the value 0 to pS->i.
   S *pS2 = new (&s, 10) S();   // C4345
   // try the following line instead
   // S *pS2 = new (&s, 10) S;   // not zero initialized
   printf("%d\n", pS2->i);
}

Community Additions

ADD
Show:
© 2014 Microsoft