What does * new mean?

M

mario.demiguel

What is the following assignment doing?

SOME_TYPE &name = *new SOME_TYPE ( *other_name);

In general, when one write "*new" What are they saying?
Thank you.
 
T

TB

(e-mail address removed) sade:
What is the following assignment doing?

SOME_TYPE &name = *new SOME_TYPE ( *other_name);

In general, when one write "*new" What are they saying?
Thank you.

Compare with:

int * pi = new int;

int & i = *pi;

TB
 
V

Victor Bazarov

What is the following assignment doing?

SOME_TYPE &name = *new SOME_TYPE ( *other_name);

In general, when one write "*new" What are they saying?

It's not an assignment. It's initialisation.

The asterisk in front of a 'new' expression immediately dereferences
the pointer the 'new' expression returns. So, the reference 'name'
is initialised to refer to the dynamic object a pointer to which the
'new' returns.

V
 
A

Andrew Brampton

What is the following assignment doing?

SOME_TYPE &name = *new SOME_TYPE ( *other_name);

In general, when one write "*new" What are they saying?
Thank you.

As the other two guys have said this dereferences the pointer to allow name
to be initialised.

One question of my own. Will this not instantly cause a memory leak since
this memory is never being deleted?

Thanks Andrew
 
S

Shezan Baig

Andrew said:
One question of my own. Will this not instantly cause a memory leak since
this memory is never being deleted?


Not if you do:

delete &name;


-shez-
 
M

mlimber

Andrew said:
As the other two guys have said this dereferences the pointer to allow name
to be initialised.

One question of my own. Will this not instantly cause a memory leak since
this memory is never being deleted?

Thanks Andrew

You could always do the equally insane

delete &name;

Better, than using a reference here, of course, would be to use a smart
pointer, e.g.,

std::auto_ptr<SOME_TYPE> name( new SOME_TYPE( *other_name ) );

Cheers! --M
 
D

Dave Townsend

mlimber said:
You could always do the equally insane

delete &name;

Better, than using a reference here, of course, would be to use a smart
pointer, e.g.,

std::auto_ptr<SOME_TYPE> name( new SOME_TYPE( *other_name ) );

Cheers! --M
Am I missing something here? Ins't this quite unnecessary complicated, just
use a real object on
the stack/global space?

SOME_TYPE name ( *other_name);

Duh?

dave
 
T

TB

Dave Townsend sade:
Am I missing something here? Ins't this quite unnecessary complicated, just
use a real object on
the stack/global space?

SOME_TYPE name ( *other_name);

Duh?

dave

Well, if you have written a lot of code using a stack object, and
suddenly that object needs to be allocated dynamically, using
the above crude syntax could save you a lot of rewriting;
changing '.' to '->', and adding dereferences ('*') where needed.
All you need to do is make sure it's deallocated either with
delete or a smart pointer.

TB
 
M

Marcelo Pinto

TB escreveu:
Well, if you have written a lot of code using a stack object, and
suddenly that object needs to be allocated dynamically, using
the above crude syntax could save you a lot of rewriting;
changing '.' to '->', and adding dereferences ('*') where needed.
All you need to do is make sure it's deallocated either with
delete or a smart pointer.

TB

In that case you could:

std::auto_ptr<SOME_TYPE> pname( new SOME_TYPE( *other_name ) );
SOME_TYPE & name = *pname;
//use name everywhere

This way you have the assurance of deletion at the end of scope (or in
face of an exception) and you don't have to bother with the
substitution.

HTH,
Marcelo Pinto
 
T

TB

Marcelo Pinto sade:
TB escreveu:


In that case you could:

std::auto_ptr<SOME_TYPE> pname( new SOME_TYPE( *other_name ) );
SOME_TYPE & name = *pname;
//use name everywhere

This way you have the assurance of deletion at the end of scope (or in
face of an exception) and you don't have to bother with the
substitution.

HTH,
Marcelo Pinto

Yes, as I said.

TB
 
M

mlimber

Dave said:
Am I missing something here? Ins't this quite unnecessary complicated, just
use a real object on
the stack/global space?

SOME_TYPE name ( *other_name);

Duh?

dave

In some cases, you need a dynamically allocated object rather than a
stack object. I was assuming the OP's case was just such a case. Of
course you should only use pointers (smart or otherwise) when you have
to.

Cheers! --M
 
D

Dave Townsend

TB said:
Dave Townsend sade:

Well, if you have written a lot of code using a stack object, and
suddenly that object needs to be allocated dynamically, using
the above crude syntax could save you a lot of rewriting;
changing '.' to '->', and adding dereferences ('*') where needed.
All you need to do is make sure it's deallocated either with
delete or a smart pointer.

TB

That's about the most unconvincing argument I've heard this week....
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top