<returnType> funct() {
<type>* ptr=<address>;
}
at the end of the function ptr goes out of scope...does it also imply deletion of the <address> it pointed to? (by deletion i mean marked free)
Printable View
<returnType> funct() {
<type>* ptr=<address>;
}
at the end of the function ptr goes out of scope...does it also imply deletion of the <address> it pointed to? (by deletion i mean marked free)
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?
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.