a question on reference

I

Ivan Liu

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
 
M

mlimber

Ivan said:
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-lite/containers.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.
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
 
K

Kai-Uwe Bux

Ivan said:
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.
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
 
M

mlimber

mlimber said:
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
 
P

Pete Becker

Kai-Uwe Bux said:
Ivan Liu wrote:




Nope. Simple rule: every new must match one and only one delete along each
possible path of flow control.




That is what you have to do.

Or, a bit more briefly,

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

peter koch

Ivan said:
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.
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<MyClass> pA (new MyClass( my_arguements));
MyClass & rA = *pA;

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

Frederick Gotham

Ivan Liu posted:
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.

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,377
Latest member
MableYocum

Latest Threads

Top