Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Dynamic Memory Allocation for References (http://www.go4expert.com/forums/dynamic-memory-allocation-references-t23056/)

rajagb4u 19Aug2010 18:03

Dynamic Memory Allocation for References
 
Hi,
I have a question regarding the references in C++.Can the references be allocated memory dynamically using malloc or new?
If yes,is it required to deallocate the memory at the end?

Prasanth

xpi0t0s 20Aug2010 12:25

Re: Dynamic Memory Allocation for References
 
Yes (assuming you mean "can a reference point to memory that is dynamically allocated").

If you allocate memory with new or malloc, it is ALWAYS required to balance that with free or delete.

LordN3mrod 7Sep2010 01:14

Re: Dynamic Memory Allocation for References
 
T* p = new T;
T& r = *p;
///r is a reference to the dynamically allocated p here
delete p; //ok, freeing memory
r.dosomething;// undefined - the referent has been destroyed!


All times are GMT +5.5. The time now is 01:34.