skydoor
skydoor

Reputation: 25898

default initialization in C++

I have a question about the default initialization in C++. I was told the non-POD object will be initialized automatically. But I am confused by the code below.

Why when I use a pointer, the variable i is initialized to 0, however, when I declare a local variable, it's not. I am using g++ as the compiler.

class INT {
    public: int i;

};

int main () {

    INT* myint1 = new INT;
    INT myint2;
    cout<<"myint1.i is "<<myint1->i<<endl;
    cout<<"myint2.i is "<<myint2.i<<endl;

    return 0;
}

The output is

myint1.i is 0

myint2.i is -1078649848

Upvotes: 3

Views: 889

Answers (7)

AnT stands with Russia
AnT stands with Russia

Reputation: 320787

Firstly, your class INT is POD.

Secondly, when something is "initialized automatically" (for automatic or dynamic objects), it means that the constructor is called automatically. There is no other "automatic" initialization scenario (for automatic or dynamic objects); all other scenarios require a "manually" specified initializer. However, if the constructor does not do anything to perform the desired initialization, then that initialization will not take place. It is your responsibility to write that constructor, when necessary.

In both cases in your example you should get garbage in your objects. The 0 that you observe in case of new-ed object is there purely by accident.

Upvotes: 2

Jonathan M Davis
Jonathan M Davis

Reputation: 38307

For a class or struct-type, if you don't tell it which constructor to use when defining a variable, then the default constructor is called. If you didn't define a default constructor, then the compiler creates one for you. If the type is not a class (or struct) type, then it is not initialized since it won't have a constructor, let alone a default constructor (so no built-in types like int will ever be default-initialized).

So, in your example, both myint1 and myint2 are default constructed with the default constuctor that the compiler declared for INT. But since that won't initialize any non-class/struct variables in an INT, the i member variable of INT is not initialized.

If you want i to be initialized, you need to write a default constructor for INT which initializes it.

Upvotes: 2

AndiDog
AndiDog

Reputation: 70239

We just had this topic, you can find some explinations here.

If you add more variables to the class, you will end up with non-initialized member variables.

Upvotes: 1

Void - Othman
Void - Othman

Reputation: 3481

That's true for non-POD object's but your object is POD since it has no user defined constructor and only contains PODs itself.

Upvotes: 1

pm100
pm100

Reputation: 50220

in both cases its not initialized, you were just lucky to get 0 in the first one

Upvotes: 6

Ricket
Ricket

Reputation: 34085

It depends on the compiler. The big difference here is that a pointer set to new Something refers to some area of memory in heap, while local variables are stored on the stack. Perhaps your compiler zeroes heap memory but doesn't bother zeroing stack memory; either way, you can't count on either method zeroing your memory. You should use something like ZeroMemory in Win32 or memset from the C standard libraries to zero your memory, or set i = 0 in the constructor of INT.

Upvotes: 2

Alexander Gessler
Alexander Gessler

Reputation: 46687

You need to declare a c'tor in INT and force 'i' to a well-defined value.

class INT {
public:

    INT() : i(0) {}

 ...
};

i is still a POD, and is thus not initialized by default. It doesn't make a difference whether you allocate on the stack or from the heap - in both cases, the value if i is undefined.

Upvotes: 8

Related Questions