473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

What does * new mean?

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.

Jan 12 '06 #1
12 1690
TB
ma************@gmail.com 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
Jan 12 '06 #2
ma************@gmail.com wrote:
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
Jan 12 '06 #3
<ma************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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
Jan 12 '06 #4
Andrew Brampton wrote:
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-

Jan 12 '06 #5
Andrew Brampton wrote:
<ma************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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


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

Jan 12 '06 #6

"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Andrew Brampton wrote:
<ma************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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


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

Jan 13 '06 #7
TB
Dave Townsend sade:
What is the following assignment doing?

SOME_TYPE &name = *new SOME_TYPE ( *other_name);
snip
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


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
Jan 13 '06 #8

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

Jan 13 '06 #9
TB
Marcelo Pinto sade:
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


Yes, as I said.

TB
Jan 13 '06 #10

Dave Townsend wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Andrew Brampton wrote:
<ma************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
> 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


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


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

Jan 13 '06 #11

"TB" <TB@void.com> wrote in message
news:43***********************@taz.nntpserver.com. ..
Dave Townsend sade:
> What is the following assignment doing?
>
> SOME_TYPE &name = *new SOME_TYPE ( *other_name);
> snip
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


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....

Jan 13 '06 #12
TB
Dave Townsend sade:
"TB" <TB@void.com> wrote in message
news:43***********************@taz.nntpserver.com. ..
Dave Townsend sade:
>> What is the following assignment doing?
>>
>> SOME_TYPE &name = *new SOME_TYPE ( *other_name);
>>

snip
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

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....


Granted.

TB
Jan 13 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
2
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table ....
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
1
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.