assert

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R2Vvcmdl?=

    #1

    assert

    Hello everyone,


    I saw a couple of form of assert in code on Windows,

    1. ASSERT;
    2. assert;
    3. _ASSERT;
    4. _assert.

    Which one is the most correct to use? I saw people always define this to
    that, and I want to find the root one which is defined by Windows.

    I also saw people manually define assert to NULL if macro DEBUG or _DEBUG is
    not defined, is that necessary?

    I feel assert is in a mess and I want to find a clear and unified way for
    this item.


    thanks in advance,
    George
  • Peter Oliphant

    #2
    Re: assert

    I created and use this class, you might find it useful. It contains some
    useful methods, pretty easy to figure out from their names. Note that I use
    'assert' to answer your question:

    #include 'assert.h'

    #define MY_DEBUG (true)

    ref class My_Debug
    {
    public:

    static void Assert( bool condition )
    {
    Assert( condition, "<no text>" ) ;
    }

    static void Show( String^ comment )
    {
    Console::WriteL ine( comment ) ;
    }

    static void Assert( bool condition, String^ info )
    {
    if ( condition ) { return ; }
    Console::WriteL ine() ;
    Console::WriteL ine( info ) ;
    Console::WriteL ine() ;
    assert(!MY_DEBU G) ; // when debug is on then assert false
    }

    static void Deny( bool condition )
    {
    Assert( !condition ) ;
    }

    static void Deny( bool condition, String^ info )
    {
    Assert( !condition, info ) ;
    }

    static void Abort( String^ info )
    {
    Assert( false, info ) ;
    }

    static void Abort()
    {
    Abort( "*abort*" ) ;
    }
    } ;

    // [==Peter==]

    "George" <George@discuss ions.microsoft. comwrote in message
    news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@mi crosoft.com...
    Hello everyone,
    >
    >
    I saw a couple of form of assert in code on Windows,
    >
    1. ASSERT;
    2. assert;
    3. _ASSERT;
    4. _assert.
    >
    Which one is the most correct to use? I saw people always define this to
    that, and I want to find the root one which is defined by Windows.
    >
    I also saw people manually define assert to NULL if macro DEBUG or _DEBUG
    is
    not defined, is that necessary?
    >
    I feel assert is in a mess and I want to find a clear and unified way for
    this item.
    >
    >
    thanks in advance,
    George

    Comment

    • Cholo Lennon

      #3
      Re: assert

      Generally I prefer to use 'assert' because is standard, but in MFC/ATL I use
      ASSERT because the debugger stop on the offending line (this is not true for
      assert). I've just used _ASSERT on a few occasions (when I used CRT debugging
      functions to find a leak).

      Be aware that:

      - assert depends of NDEBUG macro.
      - ASSERT and _ASSERT depend of _DEBUG macro.


      Regards

      --
      Cholo Lennon
      Bs.As.
      ARG


      "George" <George@discuss ions.microsoft. comwrote in message
      news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@mi crosoft.com...
      Hello everyone,
      >
      >
      I saw a couple of form of assert in code on Windows,
      >
      1. ASSERT;
      2. assert;
      3. _ASSERT;
      4. _assert.
      >
      Which one is the most correct to use? I saw people always define this to
      that, and I want to find the root one which is defined by Windows.
      >
      I also saw people manually define assert to NULL if macro DEBUG or _DEBUG is
      not defined, is that necessary?
      >
      I feel assert is in a mess and I want to find a clear and unified way for
      this item.
      >
      >
      thanks in advance,
      George

      Comment

      • =?Utf-8?B?R2Vvcmdl?=

        #4
        Re: assert

        Thanks Peter,


        The class is very useful. I have a further question, why do you add ref
        before class?


        regards,
        George

        "Peter Oliphant" wrote:
        I created and use this class, you might find it useful. It contains some
        useful methods, pretty easy to figure out from their names. Note that I use
        'assert' to answer your question:
        >
        #include 'assert.h'
        >
        #define MY_DEBUG (true)
        >
        ref class My_Debug
        {
        public:
        >
        static void Assert( bool condition )
        {
        Assert( condition, "<no text>" ) ;
        }
        >
        static void Show( String^ comment )
        {
        Console::WriteL ine( comment ) ;
        }
        >
        static void Assert( bool condition, String^ info )
        {
        if ( condition ) { return ; }
        Console::WriteL ine() ;
        Console::WriteL ine( info ) ;
        Console::WriteL ine() ;
        assert(!MY_DEBU G) ; // when debug is on then assert false
        }
        >
        static void Deny( bool condition )
        {
        Assert( !condition ) ;
        }
        >
        static void Deny( bool condition, String^ info )
        {
        Assert( !condition, info ) ;
        }
        >
        static void Abort( String^ info )
        {
        Assert( false, info ) ;
        }
        >
        static void Abort()
        {
        Abort( "*abort*" ) ;
        }
        } ;
        >
        // [==Peter==]
        >
        "George" <George@discuss ions.microsoft. comwrote in message
        news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@mi crosoft.com...
        Hello everyone,


        I saw a couple of form of assert in code on Windows,

        1. ASSERT;
        2. assert;
        3. _ASSERT;
        4. _assert.

        Which one is the most correct to use? I saw people always define this to
        that, and I want to find the root one which is defined by Windows.

        I also saw people manually define assert to NULL if macro DEBUG or _DEBUG
        is
        not defined, is that necessary?

        I feel assert is in a mess and I want to find a clear and unified way for
        this item.


        thanks in advance,
        George
        >
        >
        >

        Comment

        • =?Utf-8?B?R2Vvcmdl?=

          #5
          Re: assert

          Thanks Cholo,


          What do you mean *because the debugger stop on the offending line*? Debugger
          will stop even if the value is true?


          regards,
          George

          "Cholo Lennon" wrote:
          Generally I prefer to use 'assert' because is standard, but in MFC/ATL I use
          ASSERT because the debugger stop on the offending line (this is not true for
          assert). I've just used _ASSERT on a few occasions (when I used CRT debugging
          functions to find a leak).
          >
          Be aware that:
          >
          - assert depends of NDEBUG macro.
          - ASSERT and _ASSERT depend of _DEBUG macro.
          >
          >
          Regards
          >
          --
          Cholo Lennon
          Bs.As.
          ARG
          >
          >
          "George" <George@discuss ions.microsoft. comwrote in message
          news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@mi crosoft.com...
          Hello everyone,


          I saw a couple of form of assert in code on Windows,

          1. ASSERT;
          2. assert;
          3. _ASSERT;
          4. _assert.

          Which one is the most correct to use? I saw people always define this to
          that, and I want to find the root one which is defined by Windows.

          I also saw people manually define assert to NULL if macro DEBUG or _DEBUG is
          not defined, is that necessary?

          I feel assert is in a mess and I want to find a clear and unified way for
          this item.


          thanks in advance,
          George
          >
          >
          >

          Comment

          • Cholo Lennon

            #6
            Re: assert

            No, only when the value is false.

            I'm sorry, when I said "...debugge r stops on...", I should have said
            "...debugge r breaks on...".

            Try to debug a program using assert(false) and ASSERT(false) to see what
            happens. In the 1st case the debugger break into an internal function. In the
            2nd case it just break on the ASSERT line.

            Regards

            --
            Cholo Lennon
            Bs.As.
            ARG


            "George" <George@discuss ions.microsoft. comwrote in message
            news:46869F9D-9AAA-43D4-B3F2-17A66AEC26A0@mi crosoft.com...
            Thanks Cholo,
            >
            >
            What do you mean *because the debugger stop on the offending line*? Debugger
            will stop even if the value is true?
            >
            >
            regards,
            George
            >
            "Cholo Lennon" wrote:
            >
            Generally I prefer to use 'assert' because is standard, but in MFC/ATL I use
            ASSERT because the debugger stop on the offending line (this is not true for
            assert). I've just used _ASSERT on a few occasions (when I used CRT
            debugging
            functions to find a leak).

            Be aware that:

            - assert depends of NDEBUG macro.
            - ASSERT and _ASSERT depend of _DEBUG macro.


            Regards

            --
            Cholo Lennon
            Bs.As.
            ARG


            "George" <George@discuss ions.microsoft. comwrote in message
            news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@mi crosoft.com...
            Hello everyone,
            >
            >
            I saw a couple of form of assert in code on Windows,
            >
            1. ASSERT;
            2. assert;
            3. _ASSERT;
            4. _assert.
            >
            Which one is the most correct to use? I saw people always define this to
            that, and I want to find the root one which is defined by Windows.
            >
            I also saw people manually define assert to NULL if macro DEBUG or _DEBUG
            is
            not defined, is that necessary?
            >
            I feel assert is in a mess and I want to find a clear and unified way for
            this item.
            >
            >
            thanks in advance,
            George

            Comment

            • =?Utf-8?B?R2Vvcmdl?=

              #7
              Re: assert

              Thanks Cholo,


              It is my mistake to misunderstand your points. Now I am clear now.


              regards,
              George

              "Cholo Lennon" wrote:
              No, only when the value is false.
              >
              I'm sorry, when I said "...debugge r stops on...", I should have said
              "...debugge r breaks on...".
              >
              Try to debug a program using assert(false) and ASSERT(false) to see what
              happens. In the 1st case the debugger break into an internal function. In the
              2nd case it just break on the ASSERT line.
              >
              Regards
              >
              --
              Cholo Lennon
              Bs.As.
              ARG
              >
              >
              "George" <George@discuss ions.microsoft. comwrote in message
              news:46869F9D-9AAA-43D4-B3F2-17A66AEC26A0@mi crosoft.com...
              Thanks Cholo,


              What do you mean *because the debugger stop on the offending line*? Debugger
              will stop even if the value is true?


              regards,
              George

              "Cholo Lennon" wrote:
              Generally I prefer to use 'assert' because is standard, but in MFC/ATL I use
              ASSERT because the debugger stop on the offending line (this is not true for
              assert). I've just used _ASSERT on a few occasions (when I used CRT
              debugging
              functions to find a leak).
              >
              Be aware that:
              >
              - assert depends of NDEBUG macro.
              - ASSERT and _ASSERT depend of _DEBUG macro.
              >
              >
              Regards
              >
              --
              Cholo Lennon
              Bs.As.
              ARG
              >
              >
              "George" <George@discuss ions.microsoft. comwrote in message
              news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@mi crosoft.com...
              Hello everyone,


              I saw a couple of form of assert in code on Windows,

              1. ASSERT;
              2. assert;
              3. _ASSERT;
              4. _assert.

              Which one is the most correct to use? I saw people always define this to
              that, and I want to find the root one which is defined by Windows.

              I also saw people manually define assert to NULL if macro DEBUG or _DEBUG
              is
              not defined, is that necessary?

              I feel assert is in a mess and I want to find a clear and unified way for
              this item.


              thanks in advance,
              George
              >
              >
              >
              >
              >
              >

              Comment

              • Ben Voigt [C++ MVP]

                #8
                Re: assert


                "George" <George@discuss ions.microsoft. comwrote in message
                news:F7F3302C-C41E-4764-840C-8287505F0E3D@mi crosoft.com...
                Thanks Peter,
                >
                >
                The class is very useful. I have a further question, why do you add ref
                before class?
                That makes it a .NET class, visible from other .NET languages.

                >
                >
                regards,
                George
                >
                "Peter Oliphant" wrote:
                >
                >I created and use this class, you might find it useful. It contains some
                >useful methods, pretty easy to figure out from their names. Note that I
                >use
                >'assert' to answer your question:
                >>
                >#include 'assert.h'
                >>
                >#define MY_DEBUG (true)
                >>
                >ref class My_Debug
                >{
                > public:
                >>
                > static void Assert( bool condition )
                > {
                > Assert( condition, "<no text>" ) ;
                > }
                >>
                > static void Show( String^ comment )
                > {
                > Console::WriteL ine( comment ) ;
                > }
                >>
                > static void Assert( bool condition, String^ info )
                > {
                > if ( condition ) { return ; }
                > Console::WriteL ine() ;
                > Console::WriteL ine( info ) ;
                > Console::WriteL ine() ;
                > assert(!MY_DEBU G) ; // when debug is on then assert false
                > }
                >>
                > static void Deny( bool condition )
                > {
                > Assert( !condition ) ;
                > }
                >>
                > static void Deny( bool condition, String^ info )
                > {
                > Assert( !condition, info ) ;
                > }
                >>
                > static void Abort( String^ info )
                > {
                > Assert( false, info ) ;
                > }
                >>
                > static void Abort()
                > {
                > Abort( "*abort*" ) ;
                > }
                >} ;
                >>
                >// [==Peter==]
                >>
                >"George" <George@discuss ions.microsoft. comwrote in message
                >news:C12D5DA D-5D70-41CC-8CC3-3423482E0B41@mi crosoft.com...
                Hello everyone,
                >
                >
                I saw a couple of form of assert in code on Windows,
                >
                1. ASSERT;
                2. assert;
                3. _ASSERT;
                4. _assert.
                >
                Which one is the most correct to use? I saw people always define this
                to
                that, and I want to find the root one which is defined by Windows.
                >
                I also saw people manually define assert to NULL if macro DEBUG or
                _DEBUG
                is
                not defined, is that necessary?
                >
                I feel assert is in a mess and I want to find a clear and unified way
                for
                this item.
                >
                >
                thanks in advance,
                George
                >>
                >>
                >>

                Comment

                • Ben Voigt [C++ MVP]

                  #9
                  Re: assert


                  "Peter Oliphant" <poliphant@roun dtripllc.comwro te in message
                  news:u5Px4qYFIH A.4196@TK2MSFTN GP04.phx.gbl...
                  >I have a further question, why do you add ref before class?
                  >
                  There are two basic kinds of classes (or structs, since structs are now
                  just classes that are internally default public): reference classes and
                  value classes.
                  Everything you wrote is true about .NET types. The C++/CLI compiler
                  supports .NET types of both varieties (actually four varieties, if you
                  include interfaces and enums), and also standard C++ classes, PODs, and
                  enums. So for the C++/CLI programmer, there are many different kinds.


                  Comment

                  • =?Utf-8?B?R2Vvcmdl?=

                    #10
                    Re: assert

                    Thanks Ben,


                    regards,
                    George

                    "Ben Voigt [C++ MVP]" wrote:
                    >
                    "George" <George@discuss ions.microsoft. comwrote in message
                    news:F7F3302C-C41E-4764-840C-8287505F0E3D@mi crosoft.com...
                    Thanks Peter,


                    The class is very useful. I have a further question, why do you add ref
                    before class?
                    >
                    That makes it a .NET class, visible from other .NET languages.
                    >
                    >


                    regards,
                    George

                    "Peter Oliphant" wrote:
                    I created and use this class, you might find it useful. It contains some
                    useful methods, pretty easy to figure out from their names. Note that I
                    use
                    'assert' to answer your question:
                    >
                    #include 'assert.h'
                    >
                    #define MY_DEBUG (true)
                    >
                    ref class My_Debug
                    {
                    public:
                    >
                    static void Assert( bool condition )
                    {
                    Assert( condition, "<no text>" ) ;
                    }
                    >
                    static void Show( String^ comment )
                    {
                    Console::WriteL ine( comment ) ;
                    }
                    >
                    static void Assert( bool condition, String^ info )
                    {
                    if ( condition ) { return ; }
                    Console::WriteL ine() ;
                    Console::WriteL ine( info ) ;
                    Console::WriteL ine() ;
                    assert(!MY_DEBU G) ; // when debug is on then assert false
                    }
                    >
                    static void Deny( bool condition )
                    {
                    Assert( !condition ) ;
                    }
                    >
                    static void Deny( bool condition, String^ info )
                    {
                    Assert( !condition, info ) ;
                    }
                    >
                    static void Abort( String^ info )
                    {
                    Assert( false, info ) ;
                    }
                    >
                    static void Abort()
                    {
                    Abort( "*abort*" ) ;
                    }
                    } ;
                    >
                    // [==Peter==]
                    >
                    "George" <George@discuss ions.microsoft. comwrote in message
                    news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@mi crosoft.com...
                    Hello everyone,


                    I saw a couple of form of assert in code on Windows,

                    1. ASSERT;
                    2. assert;
                    3. _ASSERT;
                    4. _assert.

                    Which one is the most correct to use? I saw people always define this
                    to
                    that, and I want to find the root one which is defined by Windows.

                    I also saw people manually define assert to NULL if macro DEBUG or
                    _DEBUG
                    is
                    not defined, is that necessary?

                    I feel assert is in a mess and I want to find a clear and unified way
                    for
                    this item.


                    thanks in advance,
                    George
                    >
                    >
                    >
                    >
                    >
                    >

                    Comment

                    • =?Utf-8?B?R2Vvcmdl?=

                      #11
                      Re: assert

                      Thanks Ben,


                      I am using unmanaged C++ in Visual Studio 2005. Could I use such keyword
                      like ref and value? If yes, could you recommmend some learning resource
                      please?


                      regards,
                      George

                      "Ben Voigt [C++ MVP]" wrote:
                      >
                      "Peter Oliphant" <poliphant@roun dtripllc.comwro te in message
                      news:u5Px4qYFIH A.4196@TK2MSFTN GP04.phx.gbl...
                      I have a further question, why do you add ref before class?
                      There are two basic kinds of classes (or structs, since structs are now
                      just classes that are internally default public): reference classes and
                      value classes.
                      >
                      Everything you wrote is true about .NET types. The C++/CLI compiler
                      supports .NET types of both varieties (actually four varieties, if you
                      include interfaces and enums), and also standard C++ classes, PODs, and
                      enums. So for the C++/CLI programmer, there are many different kinds.
                      >
                      >
                      >

                      Comment

                      • David Wilkinson

                        #12
                        Re: assert

                        George wrote:
                        Thanks Ben,
                        >
                        >
                        I am using unmanaged C++ in Visual Studio 2005. Could I use such keyword
                        like ref and value? If yes, could you recommmend some learning resource
                        please?
                        George:

                        The keywords ref and value are for managed code (C++/CLI) only.

                        If you are using unmanaged code, you would be better posting in

                        microsoft.publi c.vc.language

                        The group

                        microsoft.publi c.dotnet.langua ges.vc

                        is for managed code.

                        --
                        David Wilkinson
                        Visual C++ MVP

                        Comment

                        • =?Utf-8?B?R2Vvcmdl?=

                          #13
                          Re: assert

                          Thanks for your clarification, David!


                          regards,
                          George

                          "David Wilkinson" wrote:
                          George wrote:
                          Thanks Ben,


                          I am using unmanaged C++ in Visual Studio 2005. Could I use such keyword
                          like ref and value? If yes, could you recommmend some learning resource
                          please?
                          >
                          George:
                          >
                          The keywords ref and value are for managed code (C++/CLI) only.
                          >
                          If you are using unmanaged code, you would be better posting in
                          >
                          microsoft.publi c.vc.language
                          >
                          The group
                          >
                          microsoft.publi c.dotnet.langua ges.vc
                          >
                          is for managed code.
                          >
                          --
                          David Wilkinson
                          Visual C++ MVP
                          >

                          Comment

                          Working...