code_junkie

assert and NDEBUG

Nov 13th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. //check later code won't crash the system
  2. if( buf.length() % 2 )
  3. return false;
  4. // do other stuff that shouldn't affect bufs length
  5. //copy 2 bytes into buf at a time, if length is odd then don't know
  6. //what will happen so use assert to make sure it can't damage anything
  7. assert( !(buf.length() % 2) );
  8. for( i = 0; i != buf.length(); i += 2 )
  9. memcpy( buf + i, data, 2 );
  10.  
  11. /// ASSERT(condition) checks if the condition is met, and if not, calls
  12. /// ABORT with an error message indicating the module and line where
  13. /// the error occurred.
  14. #ifndef ASSERT
  15. #define ASSERT(x)
  16. if (!(x)) {
  17. char buf[2048];
  18. snprintf (buf, 2048, "Assertion failed in "%s", line %dn"
  19. "tProbable bug in software.n",
  20. __FILE__, __LINE__);
  21. ABORT (buf);
  22. }
  23. else // This 'else' exists to catch the user's following semicolon
  24. #endif
  25.  
  26.  
  27. /// DASSERT(condition) is just like ASSERT, except that it only is
  28. /// functional in DEBUG mode, but does nothing when in a non-DEBUG
  29. /// (optimized, shipping) build.
  30. #ifdef DEBUG
  31. # define DASSERT(x) ASSERT(x)
  32. #else
  33. # define DASSERT(x) /* DASSERT does nothing when not debugging */
  34. #endif
  35.  
  36. #undef NDEBUG
Add Comment
Please, Sign In to add comment