Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 109,567 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,435 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Cleaning up

 
Reply to this topicStart new topic

Cleaning up

Tom9729
post 6 Aug, 2008 - 06:47 PM
Post #1


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,404



Thanked 7 times

Dream Kudos: 325
My Contributions


Perhaps this is a bad question, but is there any point in cleaning up (ie. freeing allocating memory) before a program exits?

Once the program ceases termination doesn't the memory go back to the system anyway? Or is that dependent upon the implementation?

I'm not really against doing it, I'm just curious if it's necessary.
User is online!Profile CardPM

Go to the top of the page


KYA
post 6 Aug, 2008 - 07:37 PM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 2,932



Thanked 22 times

Dream Kudos: 1150
My Contributions


General rule of thumb: clean up after mallocs/news.

C++ doesn't have an inherent/built in garbage collector so every time you don't free memory, there is potential for a memory leak. (Which could eventually lead to a system crash later on down the road).
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 7 Aug, 2008 - 02:58 PM
Post #3


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 515



Thanked 38 times
My Contributions


QUOTE(Tom9729 @ 6 Aug, 2008 - 06:47 PM) *

Once the program ceases termination doesn't the memory go back to the system anyway? Or is that dependent upon the implementation?


That is correct. In both senses. Because the operating system has to keep track of everything, it is inherently implementation dependent. However, I don't know of any modern implementations that do not do this.

I think the idea between always freeing what you malloc is that you don't know when someone may reuse your code for something where it usually matters (an interactive application that runs for more than a few seconds, for example).

The same thing with file handles is also true. Actually, this can be mostly applied to all OS resources.

Where you'll really get screwed on this though is with Windows GDI handles. All kinds of funny things happen when the program creates enough handles to prevent other applications from creating them (and they tend to leak quickly... as quickly as your leaky callbacks are called).

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 9/7/08 11:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month