the difference of automatic and dynamic variables rules in zero initialization

291 views Asked by At

code like this,

#include <iostream>
 
class obj
{
public:
    int v;
};

int main(int argc, char *argv[])
{
    obj o1; 
    std::cout << o1.v << std::endl; // print 32766, indeterminate values
    obj *o2 = new obj();
    std::cout << o2->v << std::endl; // print 0,but why?

    int v1;
    std::cout << v1 << std::endl; // print 22024, indeterminate values
    int *v2 = new int;
    std::cout << *v2 << std::endl; // print 0,but why?
    return 0;
}

I know the global or static variables will be initialize zero.
and automatic does the indeterminate values.
but the heap object use new keyword, has any reference to explain it?

3

There are 3 answers

4
user12002570 On BEST ANSWER

obj *o2 = new obj(); is value initialization meaning the object will be zero initialized and hence the data member v will be initialized to 0.

This can be seen from value initialization:

This is the initialization performed when an object is constructed with an empty initializer.

new T ()  (2)     

2,6) when an object with dynamic storage duration is created by a new-expression with the initializer consisting of an empty pair of parentheses or braces (since C++11);


On the other hand,

int *v2 = new int; //this uses default initialization
std::cout << *v2 << std::endl; //this is undefined behavior

the above leads to undefined behavior because you're dereferencing v2 and the allocated int object has is uninitialized and so has indeterminate value.

Undefined behavior means anything can happen. But never rely(or make conclusions based) on the output of a program that has UB. The program may just crash.

This can be seen from default initialization:

This is the initialization performed when an object is constructed with no initializer.

new T     (2)     

when an object with dynamic storage duration is created by a new-expression with no initializer;

The effects of default initialization are:

  • otherwise, no initialization is performed: the objects with automatic storage duration (and their subobjects) contain indeterminate values.
0
Vlad from Moscow On

From the C++ 17 Standard (11.6 Initializers)

11 An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

For fundamental types as int it means zero-initialization.

So in this declaration

obj *o2 = new obj();

the data member v of the of the dynamically allocated object is zero-initialized.

As for this code snippet

int *v2 = new int;
std::cout << *v2 << std::endl; // print 0,but why?

then the object pointed to by the pointer v2 is not initialized and the next output statement can output any value that was stored in the memory extent where the new object is allocated.

0
Goswin von Brederlow On

Initialization rules are a mess to remember. So lets just side step them and force the issue:

class obj
{
public:
    int v{};
};

There, now v is always going to be initialized. That even works if you add a Constructor that doesn't initialize v as then the member initialization is added automatically by the compiler.


As for why your code behaved as it did:

obj o1; 
std::cout << o1.v << std::endl; // print 32766, indeterminate values

o1 is created on the stack (or actually a register) and default initialized. Which for the int means no initialization. Whatever random data is on the stack (or in the register) is what you get.

obj *o2 = new obj();
std::cout << o2->v << std::endl; // print 0,but why?

From the C++ 17 Standard (11.6 Initializers):

11 An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

Value initialization of int sets it to 0.

int v1;
std::cout << v1 << std::endl; // print 22024, indeterminate values

Default initialization and you get random data.

int *v2 = new int;
std::cout << *v2 << std::endl; // print 0,but why?

Default initialization and you get random data. Freshly allocated memory just happens to be 0 unless it's reused by the application.