zoukankan      html  css  js  c++  java
  • assert()在Release模式是不会起作用的

    唉,不能道听途说,"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---------------------
    请按任意键继续. . .

  • 相关阅读:
    左孩子右兄弟的字典树
    UVA 1401 Remember the Word
    HDOJ 4770 Lights Against Dudely
    UvaLA 3938 "Ray, Pass me the dishes!"
    UVA
    Codeforces 215A A.Sereja and Coat Rack
    Codeforces 215B B.Sereja and Suffixes
    HDU 4788 Hard Disk Drive
    HDU 2095 find your present (2)
    图的连通性问题—学习笔记
  • 原文地址:https://www.cnblogs.com/tupx/p/3485128.html
Copyright © 2011-2022 走看看