a question on reference 
September 5th, 2006, 03:55 PM
| | | |
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 | 
September 5th, 2006, 04:05 PM
| | | | 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 | 
September 5th, 2006, 04:15 PM
| | | | 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 | 
September 5th, 2006, 04:15 PM
| | | | 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 | 
September 5th, 2006, 04:25 PM
| | | | 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. | 
September 5th, 2006, 04:45 PM
| | | | 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 | 
September 5th, 2006, 05:15 PM
| | | | 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 |  | | | | /bytes/about
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 225,662 network members.
|