tags:

views:

216

answers:

7

Hi, 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

+8  A: 

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.

Alexander Gessler
+1  A: 

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.

AndiDog
Note, however, when reading #1: your INT is a POD (Plain 'ol Data) type, so that rule doesn't apply.
Ricket
Yes, with only one variable it's an exception -> then it's indeed a POD type.
AndiDog
@AndiDog: It is POD not because it has "only one variable". The definition of POD does not impose any limit on the number of members in the class.
AndreyT
Thanks for clarifying that. Makes more sense that way.
AndiDog
+2  A: 

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.

AndreyT
+2  A: 

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.

Ricket
+2  A: 

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.

Jonathan M Davis
+6  A: 

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

pm100
+1  A: 

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.

Void