C Standard Library Series of Assert.h

Source: Internet
Author: User

First, a brief introduction to the <assert.h> header file, the purpose of which is to provide a macro assert definition, that can be used in the application of the program to assert processing, the role of the assertion in the program when in debug mode,
If the program gives a precondition that is not satisfied or does not meet the expected predetermined condition, the assertion is false, and the program terminates abnormally, while debugging hangs at the assertion failure (that is, where the result is false) and prints or displays the assertion
The failed message.

The

Assert function is actually a macro, as is the case with the C standard library implementations of GLIBC and Microsoft, such as glibc:
# define ASSERT (expr) \
(expr) \
? __assert_void_cas T (0) \
: __assert_fail (#expr, __file__, __line__, __assert_function)

You can see that the macro is actually using the three-mesh operator to determine whether the expression expr is true or false, and to perform the corresponding The processing, when for real execution __assert_void_cast (0), the function is actually a macro;
i.e. # define __assert_void_cast Static_cast<void> In addition, the __assert_fail function is a function that prints the message and terminates the assertion failure, which is an assertion expression,
The current file path name, the current line number, and the assertion function name, which are provided by the corresponding macro. The
__assert_fail function internally calls __assert_fail_base, which provides formatted printout, __assert_fail_base internally allocated heap space to store formatted information, and calls __ Fxprintf and Fflush to stderr
Standard output Brush string, then release the allocated heap space and invoke abort termination program; Microsoft provides a similar implementation, but it provides different implementations for Unicode and multibyte, when Unicode , whose assert macro
fails when the _assert function is called, the same _assert function internally calls _set_error_mode to control the settings output, sprintf format the error message, and then through GetStdHandle (std_error_ HANDLE) Gets the error output device
and calls Writeconsole to output the device output string information to the error, and then also calls abort to terminate the program, and when it is in multibyte mode, it calls _ Ftprintf and fflush to stderr standard output brush string;

An assert function whose argument is of type int, so long as the expression evaluates to the type int can be converted.

Assert assertion is only valid in debug mode, and its macro declaration in release release or Ndebug mode is similar to # define ASSERT (expr) (__assert_void_cast (0)) or # define ASSERT ( Ignore) ((void) 0);
That is, do nothing, also including the value of the expression is not calculated, this must be remembered! This may introduce bugs if used improperly, resulting in different performance behavior for the program in the mode and development modes.

By the way, the Abort function is also a member of the C language standard library stdlib.h.

In addition, Static_assert macros are provided in C11, which is recommended for assertion processing if the compiler supports C11, and the subsequent C standard library analysis is temporarily not for the C11 standard library.

C Standard Library Series of Assert.h

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.