Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are pointers released from memory when c++ program end?

This is a beginner question but I learned programming with c# and I am now moving to c++ and now that I am working with pointers, I know that I have to free them from memory when I'm done with them but when the program is closed are they removed from the memory or do they stay there?

like image 314
ESD Avatar asked Oct 13 '12 17:10

ESD


1 Answers

When your program ends, all the memory it used (whether dynamically allocated or not) is returned to the operating system. It doesn't matter if it's a C program, a C++ program, a C# program, or any other kind of program you might be writing.

Now, just because the OS will reclaim the memory doesn't mean you can be cavalier about memory management. While your program runs, you should try to take care of freeing any memory you're done with. Not doing so will cause "memory leaks", and those can certainly affect your program and the system it's running on, at least while your program is running.

like image 144
Carl Norum Avatar answered Oct 14 '22 17:10

Carl Norum