Returning memory at the end of a C ++ program

When a C ++ program exits, the RAM used during the run is cleared and returned to the system, right?

Question 1) Is this memory returned using C ++ language functions or the computer hardware itself?

Question 2) Does the memory get returned efficiently / safely if I end the run using ctrl+Z in a Unix terminal?

+2
source share
5 answers

When the C ++ program exits, the RAM used during the run clears and returns to the system, fix it?

Right. By system, I hope you mean the operating system.

Question 1) Is this memory returned using C ++ language functions or the computer hardware itself?

The returned memory is managed by the operating system (if I understand the question correctly). And before returning to the OS, the memory is process controlled; at a low level, which means managing various language functions, such as allocation-release mechanisms, constructors, destructors, RAII , etc.

Question 2) How efficient or safe is memory return if I exit using ctrl + Z in a Unix terminal?

Ctrl + Z pauses the process. This does not stop him. Thus, memory is not returned to the OS until the process is complete.

In linux, Ctrl + C terminates the process, then memory returns to the OS.

+7
source
  • Usually both. At the very least, assuming normal completion, destructors will execute that usually free up memory associated with these objects. As soon as your program exits, the OS will free all the memory belonging to this process.

  • Forced termination often will not start destructors, etc., but any sensible operating system is about to clean up after the process is complete, regardless of whether it did it cleanly or not. However, there are limitations, so if you used things like blocking files, they probably cannot clean them.

+5
source

Q: When the C ++ program terminates, the RAM used during the run is cleared and returned to the system, right?

A: Right. This is true for ANY program, regardless of the language in which it was written, and regardless of whether Linux or Windows or another OS

Q Is this memory controlled by C ++ features or computer hardware returned?

A: None of them: the operating system is responsible for managing process memory.

Q: Does memory work efficiently or safely if I finish running with ctrl + Z in a Unix terminal?

A: OS resources (such as memory) are freed. But you can leave the files damaged, blocked by IPC and other Bad Things that are outside the OS if you kill the program without any help.

'Hope that helps

+3
source

Is this memory returned driven by C ++ language functions or by computer hardware itself?

Both occur, assuming proper shutdown (compared to a crash or kill). The standard C / C ++ library frees up any (not leak) memory that it allocates through OS system calls, and, ultimately, the OS cleans up any missing memory.

Does memory get to return efficiently / safely if I end up running using ctrl + Z in a Unix terminal?

Ctrl-Z pauses the process on Unix. If you terminate it with kill or kill -9, the memory will be recovered (safely / efficiently) by the OS.

+2
source

They say that dynamically allocated memory is returned only to you, the programmer. For instance,

 myclass *obj = new myclass(); 

must always have appropriate

 delete obj; 

somwehere, otherwise your program will be a memory leak, which means that the operating system may think that certain parts of the memory are used when in fact it is not - after too many leaks your memory may be exhausted completely with false memory, and you won " I can’t do anything about it. "

However, β€œC ++” (effectively meaning β€œcompiler”) takes care of everything that you allocate on the stack, for example

 myclass obj; 

while your destructors actually correctly delete everything that you dynamically create inside this class.

However, in practice, if you miss memory, modern operating systems will take care of this and, as a rule, will clear it. Usually there is some kind of system where the OS can recognize which parts of the used memory you are actually using, and then just free everything there as soon as the application is completed.

Memory leaks usually only really cause problems when your application needs so much memory that it needs to be fixed from time to time, or when you continuously leak memory in a loop (like games), on systems with limited memory (like consoles )

-1
source

All Articles