Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. Expressions (EXP) > EXP31-C. Avoid side effects in assertions

EXP31-C. Avoid side effects in assertions

The assert() macro is a convenient mechanism for incorporating diagnostic tests in code (see MSC11-C, “Incorporate diagnostic tests using assertions”). Expressions used with the standard assert macro should not have side effects. Typically, the behavior of the assert macro depends on the status of the NDEBUG preprocessor symbol. If NDEBUG is undefined, the assert macro is defined to evaluate its expression argument and abort if the result of the expression is convertible to false. If NDEBUG is defined, assert is defined to be a no-op. Consequently, any side effects resulting from evaluation of the expression in the assertion are lost in non-debugging versions of the code.

Because assert is a macro, this rule is a special case of PRE31-C, “Never invoke an unsafe macro with arguments containing assignment, increment, decrement, volatile access, or function call.”


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint