Connecting Tech Pros Worldwide Help | Site Map

a question on reference

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2006, 02:55 PM
Ivan Liu
Guest
 
Posts: n/a
Default a question on reference

Hi,

I'd like to know if I declare and initialise a reference as the
following:

MyClass & rA = *( new MyClass( my_arguements) );

at the end of the routine will the memory containing the object, which
rA refers to, be freed?

Alternatively I would first declare a pointer and then at the end
delete
the pointer, like:

MyClass * pA = new MyClass( my_arguements);
MyClass & rA = *pA;

delete pA;

But I wonder if the first case the memory is freed. thanks


  #2  
Old September 5th, 2006, 03:05 PM
mlimber
Guest
 
Posts: n/a
Default Re: a question on reference

Ivan Liu wrote:
Quote:
I'd like to know if I declare and initialise a reference as the
following:
>
MyClass & rA = *( new MyClass( my_arguements) );
>
at the end of the routine will the memory containing the object, which
rA refers to, be freed?
By "end of routine" I presume you mean "end of scope", and the answer
is "No, you must delete it." However, in general, you should use a
smart pointer (e.g., std::auto_ptr, std::tr1::shared_ptr aka
boost::shared_ptr) or a container (see
http://www.parashift.com/c++-faq-lit....html#faq-34.1) instead
of manually managing memory anyway. They can be free of overhead and
provide exception safety as well as automatic clean-up.
Quote:
Alternatively I would first declare a pointer and then at the end
delete
the pointer, like:
>
MyClass * pA = new MyClass( my_arguements);
MyClass & rA = *pA;
>
delete pA;
Unlike your code above, this will not produce a memory leak.

Cheers! --M

  #3  
Old September 5th, 2006, 03:15 PM
mlimber
Guest
 
Posts: n/a
Default Re: a question on reference

mlimber wrote:
Quote:
Ivan Liu wrote:
Quote:
Alternatively I would first declare a pointer and then at the end
delete
the pointer, like:

MyClass * pA = new MyClass( my_arguements);
MyClass & rA = *pA;

delete pA;
>
Unlike your code above, this will not produce a memory leak.
....unless, of course, an exception is thrown in the (elided) code
between the new and delete.

Cheers! --M

  #4  
Old September 5th, 2006, 03:15 PM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Re: a question on reference

Ivan Liu wrote:
Quote:
Hi,
>
I'd like to know if I declare and initialise a reference as the
following:
>
MyClass & rA = *( new MyClass( my_arguements) );
>
at the end of the routine will the memory containing the object, which
rA refers to, be freed?
Nope. Simple rule: every new must match one and only one delete along each
possible path of flow control.
Quote:
>
Alternatively I would first declare a pointer and then at the end
delete
the pointer, like:
>
MyClass * pA = new MyClass( my_arguements);
MyClass & rA = *pA;
>
delete pA;
That is what you have to do.


Best

Kai-Uwe Bux
  #5  
Old September 5th, 2006, 03:25 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: a question on reference

Kai-Uwe Bux wrote:
Quote:
Ivan Liu wrote:
>
>
Quote:
>>Hi,
>>
>>I'd like to know if I declare and initialise a reference as the
>>following:
>>
>>MyClass & rA = *( new MyClass( my_arguements) );
>>
>>at the end of the routine will the memory containing the object, which
>>rA refers to, be freed?
>
>
Nope. Simple rule: every new must match one and only one delete along each
possible path of flow control.
>
>
Quote:
>>Alternatively I would first declare a pointer and then at the end
>>delete
>>the pointer, like:
>>
>>MyClass * pA = new MyClass( my_arguements);
>>MyClass & rA = *pA;
>>
>>delete pA;
>
>
That is what you have to do.
>
Or, a bit more briefly,

MyClass& rA = *(new MyClass(my_arguments));
....
delete &rA;

--
-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
  #6  
Old September 5th, 2006, 03:45 PM
peter koch
Guest
 
Posts: n/a
Default Re: a question on reference


Ivan Liu wrote:
Quote:
Hi,
>
I'd like to know if I declare and initialise a reference as the
following:
>
MyClass & rA = *( new MyClass( my_arguements) );
>
at the end of the routine will the memory containing the object, which
rA refers to, be freed?
As others have told you, it will not.
Quote:
>
Alternatively I would first declare a pointer and then at the end
delete
the pointer, like:
>
MyClass * pA = new MyClass( my_arguements);
MyClass & rA = *pA;
>
delete pA;
>
But I wonder if the first case the memory is freed. thanks
What is wrong with
MyClass A(my_arguments);
//... use A
// no need to delete pA or worry about exceptions

If you simply can't do it the "right" way, I'd recommand that you use
std::auto_ptr instead:

std::auto_ptr<MyClasspA (new MyClass( my_arguements));
MyClass & rA = *pA;

// no need to delete pA or worry about exceptions
/Peter

  #7  
Old September 5th, 2006, 04:15 PM
Frederick Gotham
Guest
 
Posts: n/a
Default Re: a question on reference

Ivan Liu posted:
Quote:
I'd like to know if I declare and initialise a reference as the
following:
>
MyClass & rA = *( new MyClass( my_arguements) );
>
at the end of the routine will the memory containing the object, which
rA refers to, be freed?

No.

Quote:
Alternatively I would first declare a pointer and then at the end
delete
the pointer, like:
>
MyClass * pA = new MyClass( my_arguements);
MyClass & rA = *pA;
>
delete pA;
>
But I wonder if the first case the memory is freed. thanks

No, it isn't; but there's nothing stopping you from doing:

int &r = *new int;

delete &r;

--

Frederick Gotham
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.