2

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

skydoor
  • 25,218
  • 52
  • 147
  • 201
  • Looks like it is related to http://stackoverflow.com/questions/2218254 – Vivin Paliath Feb 08 '10 at 19:25
  • The following provides zero-initialization in the absence of user-defined constructors: `INT myint2 = INT();` – Manuel Feb 08 '10 at 21:24
  • @Manuel That just creates a temporary INT using the compiler-defined default constructor (with i still uninitialized) and constructs myint2 with it using the compiler defined copy constructor. No extra initialization would be done than with just INT myint2; If i is 0 there, it's implementation-dependent (possibly due to debug mode), or the garbage just so happened to be 0. In either case, i has still not been initialized properly. – Jonathan M Davis Feb 08 '10 at 23:47
  • @Jonathan - I'm afraid you are wrong, this is a well known feature called scalar initialization which works for primitive types and PODs. – Manuel Feb 09 '10 at 00:06
  • @Manuel Well, upon investigation, it looks like you're right. I was unaware of scalar initialization. I question the wisdom of such a feature since defining a default constructor would then change the behavior, but that's a different issue. Thanks for the info. – Jonathan M Davis Feb 09 '10 at 00:33

7 Answers7

8

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
  • 45,603
  • 7
  • 82
  • 122
6

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

pm100
  • 48,078
  • 23
  • 82
  • 145
2

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

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
  • 33,368
  • 30
  • 112
  • 143
2

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
  • 37,181
  • 17
  • 72
  • 102
1

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.

Community
  • 1
  • 1
AndiDog
  • 68,631
  • 21
  • 159
  • 205
1

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 - Othman
  • 3,441
  • 18
  • 18