Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a memory block allocated by using operator new/malloc persist beyond end of program execution? [duplicate]

Possible Duplicate:
When you exit a C application, is the malloc-ed memory automatically freed?

This question came to my mind when i was reading about how compulsory it is to use delete/free respectively when it comes to dynamic memory allocation in C/C++. I thought if the memory allocation persisted beyond the termination of my program execution, then yes it is compulsory; otherwise, why do i have to worry about freeing up the allocated space? Isn't the OS going to free it up automatically with process termination? How right am i? My question is that can

int *ip = new int(8);

persist beyond the termination of my program?

like image 357
badmaash Avatar asked Jul 08 '12 13:07

badmaash


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 will happen if you keep using malloc () but never free ()?

If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally).

Does the heap memory gets deallocated after program execution?

Unlike stack memory, heap memory is allocated explicitly by programmers and it won't be deallocated until it is explicitly freed. To allocate heap memory in C++, use the keyword new followed by the constructor of what you want to allocate.

What happens when malloc Cannot allocate memory?

If the malloc function is unable to allocate the memory buffer, it returns NULL. Any normal program should check the pointers returned by the malloc function and appropriately handle the situation when memory could not be allocated.


2 Answers

Short answer: No.

Long answer: No. C++ will never persist memory unless you do the work to make it do so. The reason to free memory is this:

If you don't free memory, but keep allocating it, you will run out at some point. Once you run out, almost anything can happen. On Linux, maybe the OOM killer is activated and your process is killed. Maybe the OS pages you completely to disk. Maybe you give a Windows box a blue screen if you use enough memory. It can almost be thought of as undefined behavior. Also, if you leak memory, it's just siting there, unused, unreleased, and no one can use it until your process terminates.

There's another reason too. When you release memory to the allocator, the allocator might keep it around, but just mark it as usable. That means that next time you need memory, it's already sitting there waiting for you. That means there are less calls into the kernel to ask for memory, increasing performance, as context switches are very inefficient.

EDIT: The C and C++ standards don't even give a guarantee that the memory will be cleaned up by the OS after termination. Many OSes and compilers may, but there is no guarantee. Despite this, all major desktop and mobile operation systems (with the exception of probably DOS and some very old embedded systems) do clean up a processes memory after it.

like image 154
Linuxios Avatar answered Sep 22 '22 07:09

Linuxios


You do not need to release the memory back to the OS before the program exits, because the operating system will reclaim all memory that has been allocated to your process upon the termination of the process. If you allocate an object that you need up to the completion of your process, you don't have to release it.

With that said, it is still a good idea to release the memory anyway: if your program uses dynamic memory a lot, you will almost certainly need to run a memory profiler to check for memory leaks. The profiler will tell you about the blocks that you did not free at the end, and you will need to remember to ignore them. It is a lot better to keep the number of leaks at zero, for the same reason that it is good to eliminate 100% of your compiler's warnings.

like image 35
Sergey Kalinichenko Avatar answered Sep 23 '22 07:09

Sergey Kalinichenko