Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning dynamically allocated object through reference

Reply
Thread Tools

returning dynamically allocated object through reference

 
 
mundacho@gmail.com
Guest
Posts: n/a
 
      01-22-2009
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
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-22-2009
wrote:

> 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;

--
Michael
 
Reply With Quote
 
 
 
 
Zeppe
Guest
Posts: n/a
 
      01-22-2009
wrote:
> 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

 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      01-22-2009
Hi,

Zeppe wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems returning a dynamically allocated array from a fcn gobotsoup C++ 1 01-07-2008 11:31 PM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Returning a reference to an existing C++ object as a reference JustMe Perl Misc 1 08-29-2003 07:02 AM
Allocated variable returning by a fuction is unallocated automatically ? orion30 C Programming 1 07-15-2003 05:09 AM
An allocated variable returning by a fuction is unallocated automatically ? orion30 C++ 2 07-14-2003 10:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57