|
From: Per W. <pw...@ia...> - 2003-03-01 15:54:28
|
For just about all OS, the memory allocated by malloc and new will be released by the OS uppon program termination, i.e. you don't need to release it. However, there are a number of reasons for releasing it anyway: - A debug build may then check for unreleased memory, and can inform you about any memory leaks. A single variable living through the whole program isn't considered a leak. However a buffer that the application should allocate/reallocate regularly during normal operation, and that isn't deallocated is a leak, i.e. while the same instance of the program continues running, the needed memory continues to grow. - When programming in C++, the destructor run when you release your class objects might have other side effects. It might change current mouse cursor, perform flush on file system, remove temporary files, close down a non-standard graphics mode etc. - If all allocated memory gets released when not needed anymore, it is easier to look at the code and determine the useful lifespan of the different variables. There isn't any use to add code to keep a structure up to date, if nobody will ever check the values of the structure anymore. - To support code reuse. What might be a standalone program now, might very well become a sub-module in a larger project tomorrow, and then you don't want to have to pick the code to pieces to get it working. I have probably missed a couple of really important reasons but yes, i'd suggest that you try and release all malloc/calloc/new memory before ending the program, or preferably as soon as you know that you will not need the information anymore. /Per W On Sat, 1 Mar 2003, Robson Bernard wrote: > Hi coders, > > I have a question that does not want to shut up! When i code a program, do i > have to delete all variables before exiting the main function or does the > operating system cares of free all the memory allocated? If i do not delete > the variables before main function and use the program several times, may it > cause the system memory to be unavailble? > > Any input welcome! > > Thanks! > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |