Default Initialization of Scalar Types
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Default Initialization of Scalar Types

The following sample works in Visual C++ .NET 2003 as specified in the standard:

// default_initialization_of_scalar_types.cpp
struct S
{
    int i0;
    int i1;
    S() : i1()   // i1 default initialized to 0, i0 value indeterminate
    {
    } 
};

extern "C" int printf_s(const char*, ...);

int main()
{
    // points to unitialized int (value at *pi0 is indeterminate)
    int *pi0 = new int;  

    // pointer to a default initialized int (*pi1 == 0) 
    int *pi1 = new int();
    S s;
    printf_s("These variables should both be zero: %d and %d\n",
             *pi1, s.i1);
    delete pi0;
    delete pi1;
}

Output

These variables should both be zero: 0 and 0
Show:
© 2016 Microsoft