⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 freeb4exit2.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
字号:
<html><!-- Mirrored from c-faq.com/malloc/freeb4exit2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:58 GMT --><head><title></title></head><body>[Someone found it confusing that``one should not have to free all memory before exiting'',but thatit can be considered``good programming practice to explicitly free memory''.This was my reply.]<p>From: Steve Summit<br>Date: Sat, 24 Mar 2001 14:27:06 -0800<br>Subject: Re: C-FAQ book errata<!-- Message-Id: &lt;2001Mar24.1427.scs.005@aeroroot.scs.ndip.eskimo.net&gt; --><p>Well, the situation is (unfortunately) fundamentally a bitcomplicated.  I'd say you ``shouldn't have to'' free memory,and plenty of people would agree with me, but there are probablysome who would disagree, and would say that remembering to freememory is, in fact, the programmer's responsibility.  Most ``real''operating systems do take seriously their responsibility toreclaim all resources upon program exit; under these operatingsystems, everyone would agree that if a program were to neglectto free all memory before exiting and for that memory to thenbecome irretrievably lost would be a serious bug, a bug in theoperating system itself, not a bug in the application program.<p>But on the other hand, supposedly there are systems which, forwhatever reason, do not definitively reclaim memory, and underthese systems, a programmer must either explicitly free allmemory or else suffer its loss forever (or until the nextreboot, presumably).<p>Finally, quite aside from what the operating system is or isn'tguaranteed to do, there are questions of what a responsibleprogrammer ought to do.  Under some circumstances, it is a verygood idea to take the trouble to free all (or most) memory.Under some other circumstances, it is clearly more troublethan it's worth to free memory -- if a program has lots ofcomplicated, interconnected, dynamically-allocated memorystructures, and if the program is about to exit, going to greatlengths to free all the memory might add nothing more to theprogram beyond a bigger code-space footprint, a slightly longerrunning time, and a higher probability of bugs.  (How wouldyoulike to have a program crash when it was trying to exit, andto discover that it crashed while trying to free some memorywhich the operating system would have reclaimed anyway?)<p>&gt; Perhaps, this last paragraph should say more emphatically that in<br>&gt; certain situations it is important to explicitly free memory (give<br>&gt; example with little code segment ?), but in other situations, it may not<br>&gt; be a good idea (give example).<br><p>If you're using a deficient operating system which doesn't reclaimmemory at program exit, you must free your memory yourself.If your program is a long-running server which never exits,you must free memory as you're finished with it, otherwise allavailable memory will eventually ``leak away''.  If you're writinga program which does one job and exits, but which might berewritten later to run for a long time and to perform multiplerepetitions of its task, it might be easier to write thenecessary free-memory-on-task-completion code today, while thedata structures and their memory allocation strategies are allfresh in your mind.  Finally, if you're using an automated``leak tracing'' tool, which warns you about any block of memorywhich you allocate but neglect to free, and which you're using inan attempt to track down a memory leak in a portion of your codewhich is constantly allocating and freeing memory, you may findit useful to ``fix'' all of the allocations, that is, to explicitlyfree every scrap of memory you ever allocate, even those one-timeallocations which you would have been content to have theoperating system implicitly clean up for you, so that the automatictool's extra complaints abut those one-time allocations won'tmake it harder to see its complaints about the real leaks whichyou do care about tracking down and fixing.<p>In a program, on the other hand, which does one task (thoughperhaps a long, complicated task) and then exits, which allocatesa lot of dynamic memory in the process of performing that task,and which needs the majority of the memory for the duration ofthe processing of the task, trying to explicitly free it allwould be a waste of time, and in fact it could be advisable(from the standpoints of programming time, code size, run time,and reliability) not to make the attempt.</body><!-- Mirrored from c-faq.com/malloc/freeb4exit2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:58 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -