It does not. If it did, and you had multiple pointers to the same object, the result would be multiple deletion.
Raw pointers are dumb. You can google smart pointers (plenty of types of them are included by std::tr1 and C++0x) if you want a pointer that manages itself.
so if i ran such a program 1000 times the memory pointed each time (assuming new) would never be reclaimed and would result in memory leak?
No. A memory leak (and most resource leaks) cannot persist after the application exits. The operating system is ultimately in control of those things, not your program.
i get confused abt scope/validity rules while guessing how stacks are popped (during runtime) when functions start returning references and pointers..thanks fr help
i get confused abt scope/validity rules while guessing how stacks are popped (during runtime) when functions start returning references and pointers..thanks fr help
The scope rules for pointers and references are no different than any other variable type. If that pointer or reference is declared locally, that local variable ceases to exist once the function returns.
i get confused abt scope/validity rules while guessing how stacks are popped (during runtime) when functions start returning references and pointers..thanks fr help
No variable usage what so ever, pointer or otherwise, can ever leak memory. You can only get a memory leak if you actively allocate memory using one of the memory allocating functions (such as new) and then forget to deallocate it again using its matching deallocating function (in this case delete).
All memory used by a program will be reclaimed by the OS when the program ends. So no memory leak will persist after the program has finished. If you allocate memory for the purpose of using it during the whole of a program you don't have to give it back actively. This is perfectly fine and doesn't even count as a leak because there's no element of mistake involved.
Last edited by nuzzle; March 26th, 2011 at 01:43 AM.
Bookmarks