唉,不能道听途说,"assert()在Release模式也会起作用的",需要自己动手测试才行。
下面的测试证明:assert函数在Release模式是不会起作用的。
测试类CTestClass:
#pragma once #include <iostream> #include <assert.h> using namespace std; class CTestClass { public: CTestClass(UINT iPosBegin, UINT iPosEnd) { m_iPosBegin = iPosBegin; m_iPosEnd = iPosEnd; m_iExpectSize = m_iPosEnd - m_iPosBegin; assert(m_iExpectSize > 0); } ~CTestClass(){} void OutputInfo() { cout<<"------------------Output Info Begin---------------------"<<endl; cout<<"Position Begin: "<<m_iPosBegin<<endl; cout<<"Position End: "<<m_iPosEnd<<endl; cout<<"Segment Length: "<<m_iExpectSize<<endl; cout<<"------------------Output Info End---------------------"<<endl; } INT64 m_iPosBegin; INT64 m_iPosEnd; INT64 m_iExpectSize;//Mustn't use UINT64 coz it may be negative };
调用方法:
void main() { #define SAFE_DELETE_OBJECT(obj) if(obj) {delete obj; obj = NULL;} CTestClass* obj1 = new CTestClass(10, 100); if(obj1) obj1->OutputInfo(); else cout<<"Object 1 Create Failed."<<endl; CTestClass *obj2 = new CTestClass(100, 10); if(obj2) obj2->OutputInfo(); else cout<<"Object 2 Create Failed."<<endl; SAFE_DELETE_OBJECT(obj1); SAFE_DELETE_OBJECT(obj2); system("pause"); }
Release模式下测试结果:
------------------Output Info Begin---------------------
Position Begin: 10
Position End: 100
Segment Length: 90
------------------Output Info End---------------------
------------------Output Info Begin---------------------
Position Begin: 100
Position End: 10
Segment Length: -90
------------------Output Info End---------------------
请按任意键继续. . .