memory leak question

my confusion stems from not being clear on whether the leak lives within the program execution or outside of it as well ?

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. Put another way, does one need to restart the machine to free up that memory ?

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?

Thanks for responses

closed account (zb0S216C)
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
Last edited on
The OS reclaims the memory when the process exits.
The OS reclaims the memory when the process exits.
Maybe OS particular, I don't know.

We were having some fun in class with this, running the windows task manager side by side with a memory leaking program. Dynamically allocating an array: int arr = new arr[a lot] does not increase the program's memory usage. Initializing the array for (i = 0; i < a lot; i++) arr[i] = i; is when the program starts to use memory in the task manager.

We could run (and crash) the program repeatedly, so I would say with at least windows 7 that allocated memory is freed automatically. Maybe this is in the safety of the VS debugger/ console program though.
Maybe this is in the safety of the VS debugger/ console program though.

No. You wouldn't be using your computer for much longer than five minutes without running out of memory if the OS did not reclaim memory from processes that have exited (especially from those that terminate abnormally).
Last edited on
If my understanding is correct, long ago, applications had complete freedom to consume the entire machine's resources. Those days are long gone with modern operating systems. They have completely control to prevent a poorly-behaving application from affecting other processes. Multi-user, multi-process requires this kind of protection. The OS will basically fence in each process and let it run in it's own virtual space. The OS is actually in control of that space.
http://en.wikipedia.org/wiki/Protected_mode
Last edited on
Considering most larger non-critical applications have at least few leaks any OS that doesn't reclaim memory from a terminated process would likely die out pretty quickly (not to mention all the force-quits everywhere).
Topic archived. No new replies allowed.