Connecting Tech Pros Worldwide Help | Site Map

a question on reference

  #1  
Old September 5th, 2006, 03:55 PM
Ivan Liu
Guest
 
Posts: n/a
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, 04:05 PM
mlimber
Guest
 
Posts: n/a

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, 04:15 PM
mlimber
Guest
 
Posts: n/a

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, 04:15 PM
Kai-Uwe Bux
Guest
 
Posts: n/a

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, 04:25 PM
Pete Becker
Guest
 
Posts: n/a

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, 04:45 PM
peter koch
Guest
 
Posts: n/a

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, 05:15 PM
Frederick Gotham
Guest
 
Posts: n/a

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner's question on reference variable subramanian100in@yahoo.com, India answers 3 November 21st, 2007 02:15 PM
Question on Reference Wayne Wengert answers 2 November 18th, 2005 11:18 AM
Question On Reference Types thomson answers 4 November 16th, 2005 07:45 PM
Question on Pointer Indirections rs answers 7 November 14th, 2005 03:54 AM