tags:

views:

209

answers:

4
int *pi = new int(0); 

What's the significance of 0 here? Does it mean integer array of length 0?

+10  A: 

It is an initializer (constructor parameter). The newly created int will have value of 0.

Amardeep
It is an initializer. But it is not a constructor parameter. Type `int` has no constructor.
AndreyT
@AndreyT: Could you please explain? If constructor takes one argument, then how would it be differentiated from initializer? What's the actual difference between these two?
understack
@understack: "Constructor" is a special member function present in *class* types. Non-class types (like `int`) have no member functions and, of course, have no constructors. The process you are dealing with here is called *initialization*. Initialization is a non-trivial concept, it is defined differently for different types. For class types initialization usually (but not always) means a constructor call. For non-class types initialization is defined without involving any "constructors" for the aforementioned reasons.
AndreyT
+5  A: 

It means you want a pointer to an int, and for the value of that int to be 0.

I once lost far too long on a bug that turned out to be someone wrote new char(10) when they meant new char[10]. Compiler was fine with it but it caused major corruption problems, which are so hard to spot. This was 10+ years ago and we didn't have the tools we do now. Will never forget it.

Kate Gregory
A: 

No it does not mean an array of size 0. it just means a value of 0 is assigned for the int value for which the pointer that is pi.

ckv
pi is not an integer.
Arkadiy
`pi` is a pointer to `int`, not an `int`. You could have phrased your answer more accurately. What actually happens is that 0 (which is an `int`) is passed as a parameter to the copy constructor of `int`.
Eduardo León
The downvote is almost certainly because `pi` is a *pointer* to an integer, which is a critical distinction, especially for a new programmer.
Tyler McHenry
Sorry that was unintended. Of course pi is not an integer and is just a pointer to the int. In a hurry i submitted the answer
ckv
A: 

To me, the significance of 0 is also that somebody is saying,

I deliberately want this to be 0

rather than some 'default value' which [edit]in many cases is likely to also be 0 In many debug compiles will be set to 0, but in release compiles will often be the value already present in the uninitialized memory.

Greg Domjan
The default value is just "uninitialized," so there's no telling what will be there. Most implementations don't initialize raw, dynamically allocated memory before giving it to you (with the exception of debug implementations, some of which use flag values to help you track down bugs).
James McNellis
Ooops, quite right, I was thinking of Int and other classes like bool having default values like false, got myself distracted.
Greg Domjan
Actually, yes, there is a default value there - `new int()` would be value-initialized. It's only `new int` that is uninitialized.
Pavel Minaev