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---------------------
    请按任意键继续. . .

  • 相关阅读:
    [USACO][最短路]Cow Tours
    [USACO][枚举]Preface Numbering
    [USACO][枚举]Hamming Code
    [USACO][枚举]Healthy Holsteins
    [USACO][DAG上的动态规划]Sorting A Three-Valued Sequence
    [USACO][暴力]The Castle
    [USACO][枚举]Ski Course Design
    运算符重载must take either zero or one argument错误
    关于js鼠标事件综合各大浏览器能获取到坐标的属性总共以下五种
    鼠标滚轮事件封装
  • 原文地址:https://www.cnblogs.com/tupx/p/3485128.html
Copyright © 2011-2022 走看看