C++编程思想第二版第二卷读书笔记1——编写健壮的系统

<p><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">try{</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><span style=""> </span>throw 47;//throw a exception</span></span><span style="">,可以是内建类型</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><span style=""> </span>throw t; //</span></span><span style="">也可以是自定义的异常类的对象。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">} catch(int i){</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><span style=""> </span>//</span></span><span style="">捕获</span><span lang="EN-US"><span style="font-family: Calibri;">47</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}catch(T t){ //</span></span><span style="">父类可以捕获子类异常</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><span style=""> </span>//</span></span><span style="">捕获</span><span lang="EN-US"><span style="font-family: Calibri;">t</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}catch(…){//</span></span><span style="">捕获任意异常</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;"><span style=""> </span>throw;//</span></span><span style="">抛出空以释放资源,它不会再引起此</span><span lang="EN-US"><span style="font-family: Calibri;">catch</span></span><span style="">块后与此</span><span lang="EN-US"><span style="font-family: Calibri;">try</span></span><span style="">块匹配的异常捕获</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">}</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">每次只有第一个匹配的异常被捕获(因为同一时间点只可能抛出一个异常),且异常抛出位置所在块的资源将全部被回收。当异常处理块中再次抛出异常,将导致系统调用</span><span lang="EN-US"><span style="font-family: Calibri;">terminate()</span></span><span style="">函数,可以使用</span><span lang="EN-US"><span style="font-family: Calibri;">#include&lt;exception&gt;</span></span><span style="">中的</span><span lang="EN-US"><span style="font-family: Calibri;">set_terminate(</span></span><span style="">函数名</span><span lang="EN-US"><span style="font-family: Calibri;">)</span></span><span style="">自定义</span><span lang="EN-US"><span style="font-family: Calibri;">terminate()</span></span><span style="">函数。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style=""><span style="font-size: small;">当在构造函数中抛出异常时,因为构造函数没有完成,所以系统不会调用期析构函数,此时需要手动清理已经构造的部分内容所占据的资源。这里有两招可以保证资源的释放:</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">1.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">在构造函数内部捕获异常(这样就能正常调用析构函数了)</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">2.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style="font-size: small;"><span style="">使用一个</span><span lang="EN-US"><span style="font-family: Calibri;">&lt;</span></span><span style="">模板</span><span lang="EN-US"><span style="font-family: Calibri;">&gt;</span></span><span style="">类进行资源分配与回收。</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">template&lt;class T, int sz = 1&gt; class PWrap {</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>T* ptr;</span></span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">public:</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>class RangeError {}; // Exception class</span></span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>PWrap() {</span></span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>ptr = new T[sz];</span></span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>}</span></span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>~PWrap(){</span></span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>delete[] ptr;</span></span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>}</span></span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 42pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">};//This method is called Resource Acquisition Is Initialization(RAII for short)</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">所有要在构造函数内分配的资源都用如上类包装,这样无论是否完成构造,所有资源的分配的构造函数都会完成(也就保证了调用析构函数释放资源),无论调用不调用析构函数都能保证释放该释放的资源。</span><span lang="EN-US"><span style="font-family: Calibri;">C++</span></span><span style="">本身有这样一个类,在</span><span lang="EN-US"><span style="font-family: Calibri;">&lt;memory&gt;</span></span><span style="">中</span><span lang="EN-US"><span style="font-family: Calibri;">auto_ptr&lt;T&gt;</span></span><span style="">。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">try</span></span><span style="">块可以用于任意函数前(包括类的构造函数和</span><span lang="EN-US"><span style="font-family: Calibri;">main</span></span><span style="">函数)。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">标准异常类有</span><span lang="EN-US"><span style="font-family: Calibri;">logic_error</span></span><span style="">和</span><span lang="EN-US"><span style="font-family: Calibri;">runtime_error</span></span><span style="">及他们的子类在</span><span lang="EN-US"><span style="font-family: Calibri;">#include&lt;stdexcept&gt;</span></span><span style="">(这个头文件本身包含</span><span lang="EN-US"><span style="font-family: Calibri;">&lt;exception&gt;</span></span><span style="">)中。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">异常规约(</span><span lang="EN-US"><span style="font-family: Calibri;">Exception specifications</span></span><span style="">):函数声名后列出所有可能抛出的异常(</span><span lang="EN-US"><span style="font-family: Calibri;">void f() throw(A,B,C);</span></span><span style="">)如果不列出(</span><span lang="EN-US"><span style="font-family: Calibri;">void f();</span></span><span style="">)表明可以抛出任何异常,如果为空(</span><span lang="EN-US"><span style="font-family: Calibri;">void f() throw();</span></span><span style="">)表明不允许抛出异常。在模板类中不要使用异常规约。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">如果函数抛出了不允许抛出的异常,就会引发</span><span lang="EN-US"><span style="font-family: Calibri;">unexpected()</span></span><span style="">函数(可以用</span><span lang="EN-US"><span style="font-family: Calibri;">set_unexpected()</span></span><span style="">自定义),也在</span><span lang="EN-US"><span style="font-family: Calibri;">&lt;exception&gt;</span></span><span style="">头文件中。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">如果基类中</span><span lang="EN-US"><span style="font-family: Calibri;">public</span></span><span style="">函数中抛出异常类</span><span lang="EN-US"><span style="font-family: Calibri;">A</span></span><span style="">的对象,那么派生类中该函数的重写函数只有抛出</span><span lang="EN-US"><span style="font-family: Calibri;">A</span></span><span style="">或者</span><span lang="EN-US"><span style="font-family: Calibri;">A</span></span><span style="">的子类异常。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">operator=</span></span><span style="">应当使用下面模式:</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">1.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style="font-size: small;"><span style="">不是给自己赋值,如果是的话,</span><span lang="EN-US"><span style="font-family: Calibri;">go to 6</span></span><span style="">。</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 21pt; text-indent: 0cm;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">{</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">2.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">为指针数据成员分配新的空间</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">3.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">将数据从旧的空间拷贝到新的空间</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 21pt; text-indent: 0cm;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">}//clone()</span></span><span style="">函数的来源</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">4.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">删除旧的空间</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">5.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">更新对象状态:指派新的堆指针指向指针数据成员。</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">6.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style="font-size: small;"><span style="">返回</span><span lang="EN-US"><span style="font-family: Calibri;"> *this</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">The best advice for deciding when to use exceptions is to throw exceptions only when a function fails to meet its specification.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style=""><span style="font-size: small;">不使用异常的情况:</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">1.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">同步事件</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">2.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">Not for benign error conditions</span></span><span style="">(</span><span lang="EN-US"><span style="font-family: Calibri;">you have enough information to handle an error</span></span><span style="">)</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">3.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">流程控制</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">4.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">不被迫使用的情况(如打开文件失败)</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">5.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">没有使用异常的已有代码</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style=""><span style="font-size: small;">使用异常的典型场合:</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">1.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Fix the problem and retry the function that caused the exception.</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">2.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Patch things up and continue without retrying the function.</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">3.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Do whatever you can in the current context and rethrow the same exception to a higher context.</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">4.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Do whatever you can in the current context and throw a different exception to a higher context.</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">5.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Terminate the program.</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">6.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Wrap functions (especially C library functions) that use ordinary error schemes so they produce exceptions instead.</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">7.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Simplify. If your error handling scheme makes things more complicated, it is painful and annoying to use. Exceptions can be used to make error handling simpler and more effective.</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">8.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Make your library and program safer. This is a short-term investment (for debugging) and a long-term investment (for application robustness).</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style=""><span style="font-size: small;">自定义异常最好包装到自己的命名空间。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style=""><span style="font-size: small;">捕获异常类引用而不是值:</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">1.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: small;">减少复制造成的性能损失。</span></span></p>
<p class="MsoListParagraph" style=""><span style="" lang="EN-US"><span style=""><span style="font-size: small; font-family: Calibri;">2.</span><span style='font: 7pt "Times New Roman";'> </span></span></span><span style="font-size: small;"><span style="">避免</span><span lang="EN-US"><span style="font-family: Calibri;">upcasting</span></span><span style="">的切割。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">#include&lt;cassert&gt;</span></span><span style="">宏</span><span lang="EN-US"><span style="font-family: Calibri;">assert()</span></span><span style="">,返回当前情形文件名和行数,可以用编译器选项禁用它,编译器程序</span><span style="font-family: Calibri;"> <span lang="EN-US">–DNDEBUG myfile.cpp</span></span><span style="">(全局性禁用,</span><span lang="EN-US"><span style="font-family: Calibri;">assert</span></span><span style="">主要用于测试)。也可以在程序中</span><span lang="EN-US"><span style="font-family: Calibri;">#define NDEBUG</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">Testing + programming is faster than just programming.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">在实现之前先写好测试类,测试类继承自一个抽象类。测试类应该做成一个</span><span lang="EN-US"><a href="http://download.csdn.net/source/1226543"><span style="" lang="EN-US"><span lang="EN-US">测试套装</span></span></a></span><span style="">(</span><span lang="EN-US"><a href="http://download.csdn.net/source/1226543"><span style="font-family: Calibri;">Suite</span></a></span><span style="">可以一次性测试整个工程),通过一个</span><span lang="EN-US"><span style="font-family: Calibri;">addTest()</span></span><span style="">函数添加新的测试单元或</span><span lang="EN-US"><span style="font-family: Calibri;">addSuite()</span></span><span style="">来添加新的测试套装。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Calibri;">#define TRACE(ARG) cout &lt;&lt; #ARG &lt;&lt; endl; ARG//</span></span><span style="">跟踪宏,如:</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">TRACE(for(int i = 0; i &lt; 100; i++))</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">TRACE(<span style=""> </span>cout &lt;&lt; i &lt;&lt; endl;)</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style=""><span style="font-size: small;">表示:</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">cout &lt;&lt; "for(int i = 0; i &lt; 100; i++)" &lt;&lt; endl;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">for(int i = 0; i &lt; 100; i++)</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>cout &lt;&lt; "cout &lt;&lt; i &lt;&lt; endl;" &lt;&lt; endl;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">cout &lt;&lt; i &lt;&lt; endl;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">可以使用</span><span lang="EN-US"><span style="font-family: Calibri;">Trace file</span></span><span style="">代替所有的</span><span lang="EN-US"><span style="font-family: Calibri;">cout</span></span><span style="">输出,只要包含头文件</span><span lang="EN-US"><span style="font-family: Calibri;">Trace.h</span></span><span style="">并</span><span lang="EN-US"><span style="font-family: Calibri;">#define TRACEON</span></span><span style="">即可。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">//: C03:Trace.h</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Creating a trace file.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#ifndef TRACE_H</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#define TRACE_H</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#include &lt;fstream&gt;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#ifdef TRACEON</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">std::ofstream TRACEFILE__("TRACE.OUT");</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#define cout TRACEFILE__</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#endif</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">#endif // TRACE_H ///:~</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">找到内存泄漏的好方法就是在</span><span lang="EN-US"><span style="font-family: Calibri;">#include</span></span><span style="">列表最后一行(确保所有的内存分配都被监视)使用</span><span lang="EN-US"><span style="font-family: Calibri;">MemCheck.h</span></span><span style="">头文件(它重新定义了</span><span lang="EN-US"><span style="font-family: Calibri;">new</span></span><span style="">和</span><span lang="EN-US"><span style="font-family: Calibri;">new[]</span></span><span style="">):</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">//: C02:MemCheck.h</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#ifndef MEMCHECK_H</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#define MEMCHECK_H</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#include &lt;cstddef&gt;<span style=""> </span>// For size_t</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Usurp the new operator (both scalar and array versions)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">void* operator new(std::size_t, const char*, long);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">void* operator new[](std::size_t, const char*, long);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#define new new (__FILE__, __LINE__)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">extern bool traceFlag;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#define TRACE_ON() traceFlag = true</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#define TRACE_OFF() traceFlag = false</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">extern bool activeFlag;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#define MEM_ON() activeFlag = true</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#define MEM_OFF() activeFlag = false</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">#endif // MEMCHECK_H ///:~</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style=""><span style="font-size: small;">它的实现文件:</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">//: C02:MemCheck.cpp {O}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#include &lt;cstdio&gt;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#include &lt;cstdlib&gt;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#include &lt;cassert&gt;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#include &lt;cstddef&gt;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">using namespace std;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">#undef new</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Global flags set by macros in MemCheck.h</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">bool traceFlag = true;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">bool activeFlag = false;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">namespace {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Memory map entry type</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">struct Info {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>void* ptr;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>const char* file;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>long line;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">};</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Memory map data</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">const size_t MAXPTRS = 10000u;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">Info memMap[MAXPTRS];</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">size_t nptrs = 0;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Searches the map for an address</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">int findPtr(void* p) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>for(size_t i = 0; i &lt; nptrs; ++i)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>if(memMap[i].ptr == p)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>return i;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>return -1;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">void delPtr(void* p) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>int pos = findPtr(p);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>assert(pos &gt;= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>// Remove pointer from map</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>for(size_t i = pos; i &lt; nptrs-1; ++i)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>memMap[i] = memMap[i+1];</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>--nptrs;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Dummy type for static destructor</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">struct Sentinel {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>~Sentinel() {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>if(nptrs &gt; 0) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>printf("Leaked memory at:\n");</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>for(size_t i = 0; i &lt; nptrs; ++i)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>printf("\t%p (file: %s, line %ld)\n",</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>memMap[i].ptr, memMap[i].file, memMap[i].line);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>else</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>printf("No user memory leaks!\n");</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">};</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Static dummy object</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">Sentinel s;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">} // End anonymous namespace</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Overload scalar new</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">void*</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">operator new(size_t siz, const char* file, long line) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>void* p = malloc(siz);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>if(activeFlag) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>if(nptrs == MAXPTRS) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>printf("memory map too small (increase MAXPTRS)\n");</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>exit(1);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>memMap[nptrs].ptr = p;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>memMap[nptrs].file = file;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>memMap[nptrs].line = line;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>++nptrs;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>if(traceFlag) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>printf("Allocated %u bytes at address %p ", siz, p);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>printf("(file: %s, line: %ld)\n", file, line);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>return p;</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Overload array new</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">void*</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">operator new[](size_t siz, const char* file, long line) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>return operator new(siz, file, line);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Override scalar delete</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">void operator delete(void* p) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>if(findPtr(p) &gt;= 0) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>free(p);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>assert(nptrs &gt; 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>delPtr(p);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>if(traceFlag)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span><span style=""></span>printf("Deleted memory at address %p\n", p);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>else if(!p &amp;&amp; activeFlag)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>printf("Attempt to delete unknown pointer: %p\n", p);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">}</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">// Override array delete</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;">void operator delete[](void* p) {</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small;"><span style="font-family: Calibri;"><span style=""> </span>operator delete(p);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;">} ///:~</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">要使用时在</span><span lang="EN-US"><span style="font-family: Calibri;">main()</span></span><span style="">中首先调用函数</span><span lang="EN-US"><span style="font-family: Calibri;">MEM_ON();</span></span><span style="">。</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-size: small; font-family: Calibri;"></span></span></p>

你可能感兴趣的:(读书笔记)