944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1440
  • C++ RSS
Oct 2nd, 2009
0

doubt about returning references

Expand Post »
Hi All!

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

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
leoni is offline Offline
3 posts
since Oct 2009
Oct 2nd, 2009
0

Re: doubt about returning references

Quote ...
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.

Quote ...
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:
C++ Syntax (Toggle Plain Text)
  1. Vector* MakeVector()
  2. {
  3. Vector* result = new Vector();
  4.  
  5. /* some more code */
  6.  
  7. return result;
  8. }
C++ Syntax (Toggle Plain Text)
  1. Vector* v = MakeVector();
  2.  
  3. /* use v */
  4.  
  5. delete v;
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 2nd, 2009
0

Re: doubt about returning references

OK.
Thanks a lot!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
leoni is offline Offline
3 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: please help
Next Thread in C++ Forum Timeline: Quick Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC