By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,571 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,571 IT Pros & Developers. It's quick & easy.

int *iPtr = new int;

P: n/a
So I was asked what happens when new is called. I said it acquires
memory -calls the constructor -does the typecasting and returns
the pointer. If there is no memory available it throws a bad_alloc. So
this guy asked me

int *iPtr = new int;

What happens in the above case ?

I think int is a primitive data type. It wouldn't hv any constructor.
What happens here ? What is new's role here.

Regards
Ankur
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
versesane wrote:
So I was asked what happens when new is called. I said it acquires
memory -calls the constructor -does the typecasting and returns
the pointer. If there is no memory available it throws a bad_alloc. So
this guy asked me

int *iPtr = new int;

What happens in the above case ?

I think int is a primitive data type. It wouldn't hv any constructor.
What happens here ? What is new's role here.
You get a pointer with an indeterminate pointee value [5.3.4/15]:

A new-expression that creates an object of type T initializes that object
as follows:
? If the new-initializer is omitted:
- If T is a (possibly cv-qualified) non-POD class type [...]
? Otherwise, the object created has indeterminate value. [...]
[...]
Best

Kai-Uwe Bux

Jun 27 '08 #2

P: n/a
versesane wrote:
I think int is a primitive data type. It wouldn't hv any constructor.
What happens here ? What is new's role here.
Actually it has a constructor, but if you want to invoke it, you have
to do it more explicitly:

int* ptr = new int();

Without the parentheses the value is not initialized.
Jun 27 '08 #3

P: n/a
Juha Nieminen wrote:
versesane wrote:
>I think int is a primitive data type. It wouldn't hv any constructor.
What happens here ? What is new's role here.

Actually it has a constructor, but if you want to invoke it, you have
to do it more explicitly:

int* ptr = new int();
No, it doesn't have a constructor. When you create it with 'new int()',
the '()' initializer triggers _initialization_, which sets the new
object to 0. No constructor is involved here.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #4

P: n/a
Andrey Tarasevich wrote:
No, it doesn't have a constructor. When you create it with 'new int()',
the '()' initializer triggers _initialization_, which sets the new
object to 0. No constructor is involved here.
The relevant difference being?

It looks like a constructor call, it acts like a constructor call, and
it initializes the created object, as a constructor does. For all
relevant purposes it is a constructor call. You can ever give it a
parameter, like you can do to a constructor (like "new int(5)").

What the machine code generated by the compiler ends up looking like
is completely irrelevant.
Jun 27 '08 #5

P: n/a
Juha Nieminen wrote:
Andrey Tarasevich wrote:
>No, it doesn't have a constructor. When you create it with 'new int()',
the '()' initializer triggers _initialization_, which sets the new
object to 0. No constructor is involved here.

The relevant difference being?

It looks like a constructor call,
No, you got it backwards. It doesn't look like a "constructor call". C++
language has no syntax for "constructor calls". The '()' part (possibly
with arguments) is called "initializer" in C++. And the process it
triggers is called "initialization". "Construction" (and constructor
calls) is just one very specific and very particular form of
"initialization", specific to class types only. "Initialization" in C++
is significantly wider concept than "construction".
it acts like a constructor call,
The semantics of initialization is described in C++ standard. And no, it
doesn't act as a constructor call.
and
it initializes the created object, as a constructor does.
Just because constructors initialize objects doesn't mean that every
initialization somehow involves constructor. Once again,
"initialization" in C++ is significantly wider concept than "construction".
For all
relevant purposes it is a constructor call.
No, it isn't. Otherwise, for example, one logical conclusion that would
follow from this would be to expect that the very same default
constructor should be called for automatic variables of type 'int'
declared without an initializer. As we all know, it is not the case.
You can ever give it a
parameter, like you can do to a constructor (like "new int(5)").
There are lots of things in C++ that can take a parameter. That doesn't
make them into "constructors" though.
What the machine code generated by the compiler ends up looking like
is completely irrelevant.
Machine code is not involved here in any way. What I'm saying is true on
the conceptual level of C++ language.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.