| | |
doubt about returning references
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
Hi All!
I have a doubt about returning reference. I'll show you an example:
now I can call the function MakeVector to assign it result in this two ways:
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!
I have a doubt about returning reference. I'll show you an example:
C++ Syntax (Toggle Plain Text)
Vector &MakeVector() { Vector *result = new Vector<Dim>(); /*some more code*/ return *result; }
now I can call the function MakeVector to assign it result in this two ways:
C++ Syntax (Toggle Plain Text)
Vector v = MakeVector(); //first case Vector *ptrVector; //secound case *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!
•
•
•
•
in the first case: do I leave any garbage in the memory??
•
•
•
•
in the secound case: I have to call the destructor in order to not leave garbage in the memory. Right ?
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)
Vector* MakeVector() { Vector* result = new Vector(); /* some more code */ return result; }
C++ Syntax (Toggle Plain Text)
Vector* v = MakeVector(); /* use v */ delete v;
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- Changes the Case of Output Function (C++)
- Pagination script problem (JavaScript / DHTML / AJAX)
- function returning char * (C++)
- Regarding Function Returning a Value. (C)
- Exact meaning of functions returning reference (C++)
- storing references to classes (C++)
- Philosophies on Theology? (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: please help
- Next Thread: Quick Question
| Thread Tools | Search this Thread |
3g 30-nm age amd apple array assign avatar bandwidth black bluegene cache chips class classes compile cpu database ddr3 delete desktop development disk display dos dynamic echo economy energy enterprise error firefox flash function functions gecko hard hardware hp ibm ibm.news ifstream industry intel intelibm iphone java leak linux load medicine memory microsd microsoft monitor mozilla multi-core multidimensional mysql nand news objects openoffice opensource parameter pc pcm processor ps3 python ram rdimm recession record recourse recursion redhat reference return russia server speed ssd storage string sun supercomputer supercomputing technology time trends ubuntu upgrade usb variable vb.net web wikipedia working x86






