returning dynamically allocated object through reference

M

mundacho

hello,

I'd like to know if this code is going to create a memory leak in the
calling function:

ili::IntImg
&RectangleImageComparator::synchronize(const ili::IntImg &pImage)
{
ili::IntImg *tempObj = new ili::IntImg(pImage);
return *tempObj;
}


i wanted to return a reference because it's faster because the object
is not copied but passed directly, isn't it?. My question is if the
calling function will free automatically the object returned, if I
do:

// in the calling function I'd do:
ili::IntImg myVar = synchronize(image); // will myVar be destroyed
automatically???

Thanks in advance,

eD
 
M

Michael DOUBEZ

I'd like to know if this code is going to create a memory leak in the
calling function:

ili::IntImg
&RectangleImageComparator::synchronize(const ili::IntImg &pImage)
{
ili::IntImg *tempObj = new ili::IntImg(pImage);
return *tempObj;
}
i wanted to return a reference because it's faster

It is not likely to be, you however have to deference the pointer.

The only advantage would be to ensure that your function won't return
NULL (and errors are handled by exceptions).
because the object
is not copied but passed directly, isn't it?

Yes.
A pointer is also directly passed.
My question is if the > calling function will free automatically the object returned, if I
do:

// in the calling function I'd do:
ili::IntImg myVar = synchronize(image); // will myVar be destroyed
automatically???

No.
You have a memory leak here unless your constructor manages the lifetime
of its parameter.

And no gain in performance, you still have a copy.

You can do:
li::IntImg& myVar = synchronize(image);

But latter on, you would have to call:
delete &myVar;
 
Z

Zeppe

hello,

I'd like to know if this code is going to create a memory leak in the
calling function:

ili::IntImg
&RectangleImageComparator::synchronize(const ili::IntImg &pImage)
{
ili::IntImg *tempObj = new ili::IntImg(pImage);
return *tempObj;
}


i wanted to return a reference because it's faster because the object
is not copied but passed directly, isn't it?.

no, it isn't. A object reference may be faster than the corresponding
object (it's not always the case), but returning a pointer is as fast as
returning a reference.
My question is if the
calling function will free automatically the object returned, if I
do:

// in the calling function I'd do:
ili::IntImg myVar = synchronize(image); // will myVar be destroyed
automatically???

No, you have to delete the object you allocated manually to prevent
memory leaks.

Best wishes,

Zeppe
 
M

Marcel Müller

Hi,
no, it isn't. A object reference may be faster than the corresponding
object (it's not always the case), but returning a pointer is as fast as
returning a reference.

I do not agree in general. If you have implicit up-casts together with
multiple inheritance the pointer has to be adjusted by the offset of the
base class in the derived one. But this only applies to non null
pointers. Since references must not be null the compiler can ommit this
check.


Marcel
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top