Menu

assert with 4.9.8.1 with gcc 3.2?

SagaEterna
2003-07-17
2012-09-26
  • SagaEterna

    SagaEterna - 2003-07-17

    i can't make it work.

    anyone know how to use it?

    Thanks
    Stef

     
    • Larry Young

      Larry Young - 2003-07-17

      Please be more specific!  Give us a *small* piece of code that "doesn't work" and tell us what doesn't work - compile error? link error? wrong result? no result?  Also consider posting your compile log; some assert() problems are caused by debug and optimization levels.

       
    • Anonymous

      Anonymous - 2003-07-17

      assert( <expression> ) ;

      If the result of <expression> is non zero (or false for a boolean expression), a debug message is displayed (and execution halts). The message is of the form:

      "Assertion failed: <expression>, <sourcefile>, <linenum>"

      assert has no effect if NDEBUG is defined.

      Unfortunately in a console app, because the program terminates, its window closes immediatly. Run the code form a command line window to prevent this.

      Alternatively assert is a macro not a function, which means you can modify it or write your own. This is my solution to the window closing problem:

      #if !defined NDEBUG
      #include <stdio.h>
      #undef assert
      #define assert(e)       if(e){ printf( "Assertion failed: %s %s:%d", #e, __FILE__, __LINE__); \
                               getchar() ; exit(1); }
      #else
      #define assert(e)
      #endif

      Place this in a header called <debug.h> for example, and include it where necessary.

      Clifford

       
    • Chuck Simmons

      Chuck Simmons - 2003-07-24

      Clifford -- thanks for the nice write up.

      For my wishlist, I would like functionality more like what I'm used to from unix.  When I'm running "under a debugger", assert should cause the debugger to receive control so that I can perform stack trace backs and dump variables.

       
    • Anonymous

      Anonymous - 2003-07-24

      I thought that raise(SIGTRAP) might work to invoke the debugger but Windows does not support it.

      BTW: where I used exit(1), abort() or raise(SIGABRT) may be more appropriate. Although the results are the same.

      Clifford

       

Log in to post a comment.

MongoDB Logo MongoDB