doubt about returning references

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 3
Reputation: leoni is an unknown quantity at this point 
Solved Threads: 0
leoni leoni is offline Offline
Newbie Poster

doubt about returning references

 
0
  #1
Oct 2nd, 2009
Hi All!

I have a doubt about returning reference. I'll show you an example:

  1. Vector &MakeVector()
  2. {
  3. Vector *result = new Vector<Dim>();
  4.  
  5. /*some more code*/
  6.  
  7. return *result;
  8. }

now I can call the function MakeVector to assign it result in this two ways:

  1. Vector v = MakeVector(); //first case
  2.  
  3. Vector *ptrVector; //secound case
  4. *ptrVector = MakeVector();

I have some question about it:
in the first case: do I leave any garbage in the memory?? because I'm creating an object with the new operator, so I think that i have to free it in some place. But I know that in this case is not necessary to call the destructor because the destructor of v is called automatically. So I don't know if I'm leaving garbage in the memory.

in the secound case: I have to call the destructor in order to not leave garbage in the memory. Right ?

thanks before hand!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: doubt about returning references

 
0
  #2
Oct 2nd, 2009
in the first case: do I leave any garbage in the memory??
Yes. You return a reference to a Vector, then use the copy constructor to make a copy of it in v. It causes a memory leak because you lose the reference.

in the secound case: I have to call the destructor in order to not leave garbage in the memory. Right ?
The second case will probably crash because the pointer is uninitialized, but if you initialize the pointer, you still have the same problem of needing to save the address returned by the new operator so that you can delete it and not have a memory leak.

Your design of MakeVector is the root cause of these problems. It is confusing and dangerous. If you want to allocate dynamic memory, return a pointer instead of a reference. That way it is more obvious that the memory may need to be deleted and you do not need to use tricks to get the address:
  1. Vector* MakeVector()
  2. {
  3. Vector* result = new Vector();
  4.  
  5. /* some more code */
  6.  
  7. return result;
  8. }
  1. Vector* v = MakeVector();
  2.  
  3. /* use v */
  4.  
  5. delete v;
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: leoni is an unknown quantity at this point 
Solved Threads: 0
leoni leoni is offline Offline
Newbie Poster

Re: doubt about returning references

 
0
  #3
Oct 2nd, 2009
OK.
Thanks a lot!!
Reply With Quote Quick reply to this message  
Reply

Tags
memory, reference, return

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC