-
March 25th, 2011, 04:44 PM
#1
scope rule for pointers
<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)
-
March 25th, 2011, 05:06 PM
#2
Re: scope rule for pointers
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.
-
March 25th, 2011, 05:13 PM
#3
Re: scope rule for pointers
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?
-
March 25th, 2011, 05:21 PM
#4
Re: scope rule for pointers
 Originally Posted by ustulation
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.
-
March 25th, 2011, 05:32 PM
#5
Re: scope rule for pointers
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
-
March 25th, 2011, 06:03 PM
#6
Re: scope rule for pointers
 Originally Posted by ustulation
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.
Regardsm
Paul McKenzie
-
March 25th, 2011, 11:34 PM
#7
Re: scope rule for pointers
 Originally Posted by ustulation
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|