Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I like to define my own assertion macros. I make two -- ASSERT tests always (even for optimized builds) and DASSERT only has an effect for debug builds. You probably want to default to ASSERT, but if something is expensive to test, or assertions inside inner loops of performance-sensitive areas can be changed to DASSERT.</p> <p>Also, remember, that assertions should only be used for totally nonsensical conditions that indicate a logical error in your program and from which you can't recover. It's a test of your programming correctness. Assertions should NEVER be used in place of error handling, exceptions, or robustness, and you should never assert anything related to malformed or incorrect user input -- such things should be handled gracefully. An assertion is just a controlled crash where you have the opportunity to output some extra debugging info.</p> <p>Here are my macros:</p> <pre><code>/// ASSERT(condition) checks if the condition is met, and if not, calls /// ABORT with an error message indicating the module and line where /// the error occurred. #ifndef ASSERT #define ASSERT(x) \ if (!(x)) { \ char buf[2048]; \ snprintf (buf, 2048, "Assertion failed in \"%s\", line %d\n" \ "\tProbable bug in software.\n", \ __FILE__, __LINE__); \ ABORT (buf); \ } \ else // This 'else' exists to catch the user's following semicolon #endif /// DASSERT(condition) is just like ASSERT, except that it only is /// functional in DEBUG mode, but does nothing when in a non-DEBUG /// (optimized, shipping) build. #ifdef DEBUG # define DASSERT(x) ASSERT(x) #else # define DASSERT(x) /* DASSERT does nothing when not debugging */ #endif </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload