Connecting Tech Pros Worldwide Help | Site Map

a question on reference

Ivan Liu
Guest
 
Posts: n/a
#1: Sep 5 '06
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

mlimber
Guest
 
Posts: n/a
#2: Sep 5 '06

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

mlimber
Guest
 
Posts: n/a
#3: Sep 5 '06

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

Kai-Uwe Bux
Guest
 
Posts: n/a
#4: Sep 5 '06

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
Pete Becker
Guest
 
Posts: n/a
#5: Sep 5 '06

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.
peter koch
Guest
 
Posts: n/a
#6: Sep 5 '06

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

Frederick Gotham
Guest
 
Posts: n/a
#7: Sep 5 '06

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