Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically allocated memory after program termination

When a C/C++ program containing the dynamically allocated memory(using malloc/new) without free/delete calls is terminated, what happens to that dynamically allocated memory? Does the operating system takes back the memory or does that memory becomes unaccessible to other programs?

like image 383
justin waugh Avatar asked Jul 17 '11 23:07

justin waugh


People also ask

What happens to the allocated memory after a program exits?

The memory is reclaimed by the Operating system once your program exits. The OS doesn't understand that your program leaked memory, it simply allocates memory to the program for running and once the program exits it reclaims that memory.

What happens when memory dynamically allocated at runtime is never released even after program termination?

If you allocate memory and doesn't free it up, it's of no use while the program execution continues. This is called memory leak.

What happens to dynamically allocated memory?

Dynamic memory allocation is the process of assigning the memory space during the execution time or the run time. Reasons and Advantage of allocating memory dynamically: When we do not know how much amount of memory would be needed for the program beforehand.

What happens if a dynamically allocated memory is not deleted properly?

As a result, this dynamically allocated integer can not be deleted. This is called a memory leak. Memory leaks happen when your program loses the address of some bit of dynamically allocated memory before giving it back to the operating system.


1 Answers

I don't think that there are any guarantees in the language standard, but modern operating systems which support sparse virtual memory and memory protection (such as MacOS X, Linux, all recent version of Windows, and all currently manufactured phone handsets) automatically clean up after badly-behaved processes (when they terminate) and free the memory for you. The memory remains unavailable, however as long as the program is running.

If you're programming on microcontrollers, on MacOS 9 or earler, DOS, or Windows 3.x, then you might need to be concerned about memory leaks making memory permenantly unavailable to the whole operating system.

like image 193
Ken Bloom Avatar answered Sep 23 '22 07:09

Ken Bloom