[C ++] ndebug macro and Assert Behavior

Source: Internet
Author: User
ArticleDirectory
    • Reference

 

    1. The ndebug macro is a macro defined in standard C and is used to control assert () behavior. If this macro is defined, assert does not work.
    2. # Ifdef ndebug
      # Define assert (x) (void) 0)
      # Else
      ...

    3. In C standard, assert is implemented using macros. <Assert. h> it is designed to be included for multiple times. When it comes to UNDEF assert, The ndebug macro determines its behavior. For example:
    4. # Ifdef ndebug
      # UNDEF ndebug
      # Include <assert. h>
      # Define ndebug
      # Else
      # Include <assert. h>
      # Endif // # ifdef ndebug

      Replace the aboveCodePut it at the top of C/CPP (of course, no other # include <assert. h> statement) to ensure that the assert in the compilation unit where c/CPP is located will also trigger the abort action under release.

    5. Ndebug macro is also used as a macro for judging the debug/release version due to its literal meaning. However, it is related to the compiler and environment and is not reliable.
    6. For example, in VC, this macro is defined by default for the generated release version project, but GCC is not defined, and it must be defined using the-dndebug parameter.

    7. C ++ standard fully uses the definition in C.
Reference
    • C-approved standards
      Http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899
    • C ++ standard (ISO/IEC 14882)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.