int *iPtr = new int;

V

versesane

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
 
K

Kai-Uwe Bux

versesane said:
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
 
J

Juha Nieminen

versesane said:
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.
 
A

Andrey Tarasevich

Juha said:
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.
 
J

Juha Nieminen

Andrey said:
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.
 
A

Andrey Tarasevich

Juha said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top