helterskelter wrote: |
---|
"my confusion stems from not being clear on whether the leak lives within the program execution or outside of it as well ?" |
When memory is allocated by a program, the program is given ownership of the allocated memory. At this point, the program has full control (with some limitations) over the memory. Now, when the run-time ends, and the memory remains, the OS still thinks it's your program's memory. Thus, the OS will not touch the memory until you give it back. However, this cannot be done because your program's run-time had come to a close. I tend to call deallocated memory
"Rebel Memory".
During run-time, it's possible to allocate memory within a new stack-frame or scope. This opens a window of opportunity for a memory leak. Failure to deallocate memory in either scope or stack-frame will provoke a leak during run-time.
So all-in-all, a leak can occur during both run-time and after run-time.
helterskelter wrote: |
---|
"my question is if i create a dynamic array in my program, but fail to delete the memory before the program terminates, does the memory remain allocated even after program termination." |
Yes, until either...
1) ...the operating system catches and releases the memory, or...
2) ...there's no memory left.
helterskelter wrote: |
---|
"Put another way, does one need to restart the machine to free up that memory ?"
|
Pretty much.
helterskelter wrote: |
---|
"In addition, i have noticed a convention wherein after a dynamically allocated pointer is deleted, it is made to point to 0. what precisely does this do?" |
It sets the pointer to
NULL, which is equivalent to the very first address in memory. It's not necessary, but it does prevent the memory from being deallocated twice. Since a
"Dangling Pointer" points to something, it's too easy to assume it's pointing to something that's dynamically allocated, or that it points to something that belongs to your program.
Wazzak