user-0 提问: assert断言引申的一系列问题

回复:
user: user-1
content: 另外assert是调用的abort

user: user-1
content: 断言在debug版本中会编译成目标代码，release版本中就没有了（它是个宏，release中被略去了）。  简单点说，return（main中的）和exit()是一样的，不过就是main return a（0或者1）以后，CRT的函数接受这个a，然后调用exit(a)，即exit是你调还是CRT的函数来调； exit和abort的区别就是exit打扫卫生，abort不打扫  abort -- cause abnormal program termination      The abort() function causes abnormal program termination to occur, unless      the signal SIGABRT is being caught and the signal handler does not      return.       Any open streams are flushed and closed.  exit, _Exit -- perform normal program termination      The exit() and _Exit() functions terminate a process.       Before termination, exit() performs the following functions in the order      listed:             1.   Call the functions registered with the atexit(3) function, in                 the reverse order of their registration.             2.   Flush all open output streams.             3.   Close all open streams.             4.   Unlink all files created with the tmpfile(3) function.       The _Exit() function terminates without calling the functions registered      with the atexit(3) function, and may or may not perform the other actions      listed.  详细的还可以看看APUE这样的书。

user: user-2
content: 引用 2 楼 u012072837 的回复:[quote=引用 1 楼 shenchenman 的回复:] 1 断言有中断的效果 2 这种退出方式指的是什么？断言不需要退出 3 return是退出函数，exit是退出进程  1、请问有中断的效果是神马意思？ 2、断言不是判断错误就马上退出吗？ 3、这个大概能理解，那abort呢？[/quote]  1 中断就是控制权交给操作系统，程序暂时挂起 2 断言可以选继续retry 3 abort也是退出进程，但是比exit弱。它不记录状态

user: user-3
content: assert这类功能的特点是在Debug版中不符合断言则中断执行，在Release版中不编译到目标代码中。

user: user-0
content: 引用 5 楼 zhao4zhong1 的回复:_ASSERT, _ASSERTE Macros Evaluate an expression and generate a debug report when the result is FALSE (debug version only).  _ASSERT( booleanExpression );  _ASSERTE( booleanExpression );  Macro Required Header Compatibility  _ASSERT <crtdbg.h> Win NT, Win 95  _ASSERTE <crtdbg.h> Win NT, Win 95    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBCD.LIB Single thread static library, debug version  LIBCMTD.LIB Multithread static library, debug version  MSVCRTD.LIB Import library for MSVCRTD.DLL, debug version    Although _ASSERT and _ASSERTE are macros and are obtained by including CRTDBG.H, the application must link with one of the libraries listed above because these macros call other run-time functions.  Return Value  None  Parameter  booleanExpression  Expression (including pointers) that evaluates to nonzero or 0  Remarks  The _ASSERT and _ASSERTE macros provide an application with a clean and simple mechanism for checking assumptions during the debugging process. They are very flexible because they do not need to be enclosed in #ifdef statements to prevent them from being called in a retail build of an application. This flexibility is achieved by using the _DEBUG macro. _ASSERT and _ASSERTE are only available when _DEBUG is defined. When _DEBUG is not defined, calls to these macros are removed during preprocessing.  _ASSERT and _ASSERTE evaluate their booleanExpression argument and when the result is FALSE (0), they print a diagnostic message and call _CrtDbgReport to generate a debug report. The _ASSERT macro prints a simple diagnostic message, while _ASSERTE includes a string representation of the failed expression in the message. These macros do nothing when booleanExpression evaluates to nonzero.  Because the _ASSERTE macro specifies the failed expression in the generated report, it enables users to identify the problem without referring to the application source code. However, a disadvantage exists in that every expression evaluated by _ASSERTE must be included in the debug version of your application as a string constant. Therefore, if a large number of calls are made to _ASSERTE, these expressions can take up a significant amount of space.  _CrtDbgReport generates the debug report and determines its destination(s), based on the current report mode(s) and file defined for the _CRT_ASSERT report type. By default, assertion failures and errors are directed to a debug message window. The _CrtSetReportMode and _CrtSetReportFile functions are used to define the destination(s) for each report type.  When the destination is a debug message window and the user chooses the Retry button, _CrtDbgReport returns 1, causing the _ASSERT and _ASSERTE macros to start the debugger, provided that “just-in-time” (JIT) debugging is enabled. See Debug Reporting Functions of the C Run-Time Library for an example of an assert message box under Windows NT.  For more information about the reporting process, see the _CrtDbgReport function and the section Debug Reporting Functions of the C Run-Time Library. For more information about resolving assertion failures and using these macros as a debugging error handling mechanism, see Using Macros for Verification and Reporting.  The _RPT, _RPTF debug macros are also available for generating a debug report, but they do not evaluate an expression. The _RPT macros generate a simple report and the _RPTF macros include the source file and line number where the report macro was called, in the generated report. In addition to the _ASSERTE macros, the ANSI assert routine can also be used to verify program logic. This routine is available in both the debug and release versions of the libraries.  Example  /*   * DBGMACRO.C   * In this program, calls are made to the _ASSERT and _ASSERTE   * macros to test the condition 'string1 == string2'.  If the   * condition fails, these macros print a diagnostic message.   * The _RPTn and _RPTFn group of macros are also exercised in   * this program, as an alternative to the printf function.   */  #include <stdio.h>  #include <string.h>  #include <malloc.h>  #include <crtdbg.h>  int main()  {  char *p1, *p2;  /*  * The Reporting Mode and File must be specified  * before generating a debug report via an assert  * or report macro.  * This program sends all report types to STDOUT  */  _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);  _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);  /*  * Allocate and assign the pointer variables  */  p1 = (char *)malloc(10);  strcpy(p1, "I am p1");  p2 = (char *)malloc(10);  strcpy(p2, "I am p2");  /*  * Use the report macros as a debugging  * warning mechanism, similar to printf.  *  * Use the assert macros to check if the  * p1 and p2 variables are equivalent.  *  * If the expression fails, _ASSERTE will  * include a string representation of the  * failed expression in the report.  * _ASSERT does not include the  * expression in the generated report.  */  _RPT0(_CRT_WARN, "\n\n Use the assert macros to evaluate the expression p1 == p2.\n");  _RPTF2(_CRT_WARN, "\n Will _ASSERT find '%s' == '%s' ?\n", p1, p2);  _ASSERT(p1 == p2);  _RPTF2(_CRT_WARN, "\n\n Will _ASSERTE find '%s' == '%s' ?\n", p1, p2);  _ASSERTE(p1 == p2);  _RPT2(_CRT_ERROR, "\n \n '%s' != '%s'\n", p1, p2);   free(p2);  free(p1);  return 0;  }   Output  Use the assert macros to evaluate the expression p1 == p2.  dbgmacro.c(54) : Will _ASSERT find 'I am p1' == 'I am p2' ? dbgmacro.c(55) : Assertion failed   dbgmacro.c(57) : Will _ASSERTE find 'I am p1' == 'I am p2' ? dbgmacro.c(58) : Assertion failed: p1 == p2   'I am p1' != 'I am p2'   Debug Functions  See Also   _RPT, _RPTF看了一下，0.0好像主要在用法上解释的比较多，请问有没有您自己在这方面的使用经验呢？

user: user-3
content: _ASSERT, _ASSERTE Macros Evaluate an expression and generate a debug report when the result is FALSE (debug version only).  _ASSERT( booleanExpression );  _ASSERTE( booleanExpression );  Macro Required Header Compatibility  _ASSERT <crtdbg.h> Win NT, Win 95  _ASSERTE <crtdbg.h> Win NT, Win 95    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBCD.LIB Single thread static library, debug version  LIBCMTD.LIB Multithread static library, debug version  MSVCRTD.LIB Import library for MSVCRTD.DLL, debug version    Although _ASSERT and _ASSERTE are macros and are obtained by including CRTDBG.H, the application must link with one of the libraries listed above because these macros call other run-time functions.  Return Value  None  Parameter  booleanExpression  Expression (including pointers) that evaluates to nonzero or 0  Remarks  The _ASSERT and _ASSERTE macros provide an application with a clean and simple mechanism for checking assumptions during the debugging process. They are very flexible because they do not need to be enclosed in #ifdef statements to prevent them from being called in a retail build of an application. This flexibility is achieved by using the _DEBUG macro. _ASSERT and _ASSERTE are only available when _DEBUG is defined. When _DEBUG is not defined, calls to these macros are removed during preprocessing.  _ASSERT and _ASSERTE evaluate their booleanExpression argument and when the result is FALSE (0), they print a diagnostic message and call _CrtDbgReport to generate a debug report. The _ASSERT macro prints a simple diagnostic message, while _ASSERTE includes a string representation of the failed expression in the message. These macros do nothing when booleanExpression evaluates to nonzero.  Because the _ASSERTE macro specifies the failed expression in the generated report, it enables users to identify the problem without referring to the application source code. However, a disadvantage exists in that every expression evaluated by _ASSERTE must be included in the debug version of your application as a string constant. Therefore, if a large number of calls are made to _ASSERTE, these expressions can take up a significant amount of space.  _CrtDbgReport generates the debug report and determines its destination(s), based on the current report mode(s) and file defined for the _CRT_ASSERT report type. By default, assertion failures and errors are directed to a debug message window. The _CrtSetReportMode and _CrtSetReportFile functions are used to define the destination(s) for each report type.  When the destination is a debug message window and the user chooses the Retry button, _CrtDbgReport returns 1, causing the _ASSERT and _ASSERTE macros to start the debugger, provided that “just-in-time” (JIT) debugging is enabled. See Debug Reporting Functions of the C Run-Time Library for an example of an assert message box under Windows NT.  For more information about the reporting process, see the _CrtDbgReport function and the section Debug Reporting Functions of the C Run-Time Library. For more information about resolving assertion failures and using these macros as a debugging error handling mechanism, see Using Macros for Verification and Reporting.  The _RPT, _RPTF debug macros are also available for generating a debug report, but they do not evaluate an expression. The _RPT macros generate a simple report and the _RPTF macros include the source file and line number where the report macro was called, in the generated report. In addition to the _ASSERTE macros, the ANSI assert routine can also be used to verify program logic. This routine is available in both the debug and release versions of the libraries.  Example  /*   * DBGMACRO.C   * In this program, calls are made to the _ASSERT and _ASSERTE   * macros to test the condition 'string1 == string2'.  If the   * condition fails, these macros print a diagnostic message.   * The _RPTn and _RPTFn group of macros are also exercised in   * this program, as an alternative to the printf function.   */  #include <stdio.h>  #include <string.h>  #include <malloc.h>  #include <crtdbg.h>  int main()  {  char *p1, *p2;  /*  * The Reporting Mode and File must be specified  * before generating a debug report via an assert  * or report macro.  * This program sends all report types to STDOUT  */  _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);  _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);  /*  * Allocate and assign the pointer variables  */  p1 = (char *)malloc(10);  strcpy(p1, "I am p1");  p2 = (char *)malloc(10);  strcpy(p2, "I am p2");  /*  * Use the report macros as a debugging  * warning mechanism, similar to printf.  *  * Use the assert macros to check if the  * p1 and p2 variables are equivalent.  *  * If the expression fails, _ASSERTE will  * include a string representation of the  * failed expression in the report.  * _ASSERT does not include the  * expression in the generated report.  */  _RPT0(_CRT_WARN, "\n\n Use the assert macros to evaluate the expression p1 == p2.\n");  _RPTF2(_CRT_WARN, "\n Will _ASSERT find '%s' == '%s' ?\n", p1, p2);  _ASSERT(p1 == p2);  _RPTF2(_CRT_WARN, "\n\n Will _ASSERTE find '%s' == '%s' ?\n", p1, p2);  _ASSERTE(p1 == p2);  _RPT2(_CRT_ERROR, "\n \n '%s' != '%s'\n", p1, p2);   free(p2);  free(p1);  return 0;  }   Output  Use the assert macros to evaluate the expression p1 == p2.  dbgmacro.c(54) : Will _ASSERT find 'I am p1' == 'I am p2' ? dbgmacro.c(55) : Assertion failed   dbgmacro.c(57) : Will _ASSERTE find 'I am p1' == 'I am p2' ? dbgmacro.c(58) : Assertion failed: p1 == p2   'I am p1' != 'I am p2'   Debug Functions  See Also   _RPT, _RPTF

user: user-3
content: assert Evaluates an expression and when the result is FALSE, prints a diagnostic message and aborts the program.  void assert( int expression );  Routine Required Header Compatibility  assert <assert.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  None  Parameter  expression  Expression (including pointers) that evaluates to nonzero or 0  Remarks  The ANSI assert macro is typically used to identify logic errors during program development, by implementing the expression argument to evaluate to false only when the program is operating incorrectly. After debugging is complete, assertion checking can be turned off without modifying the source file by defining the identifier NDEBUG. NDEBUG can be defined with a /D command-line option or with a #define directive. If NDEBUG is defined with #define, the directive must appear before ASSERT.H is included.  assert prints a diagnostic message when expression evaluates to false (0) and calls abort to terminate program execution. No action is taken if expression is true (nonzero). The diagnostic message includes the failed expression and the name of the source file and line number where the assertion failed.  The destination of the diagnostic message depends on the type of application that called the routine. Console applications always receive the message via stderr. In a single- or multithreaded Windows application, assert calls the Windows MessageBox API to create a message box to display the message along with an OK button. When the user chooses OK, the program aborts immediately.  When the application is linked with a debug version of the run-time libraries, assert creates a message box with three buttons: Abort, Retry, and Ignore. If the user selects Abort, the program aborts immediately. If the user selects Retry, the debugger is called and the user can debug the program if Just-In-Time (JIT) debugging is enabled. If the user selects Ignore, assert continues with its normal execution: creating the message box with the OK button. Note that choosing Ignore when an error condition exists can result in “undefined behavior.” For more information, see Using C Run-Time Library Debugging Support.  The assert routine is available in both the release and debug versions of the C run-time libraries. Two other assertion macros, _ASSERT and _ASSERTE, are also available, but they only evaluate the expressiosn passed to them when the _DEBUG flag has been defined.   Example  /* ASSERT.C: In this program, the analyze_string function uses  * the assert function to test several conditions related to  * string and length. If any of the conditions fails, the program  * prints a message indicating what caused the failure.  */  #include <stdio.h> #include <assert.h> #include <string.h>  void analyze_string( char *string );   /* Prototype */  void main( void ) {    char  test1[] = "abc", *test2 = NULL, test3[] = "";     printf ( "Analyzing string '%s'\n", test1 );    analyze_string( test1 );    printf ( "Analyzing string '%s'\n", test2 );    analyze_string( test2 );    printf ( "Analyzing string '%s'\n", test3 );    analyze_string( test3 ); }  /* Tests a string to see if it is NULL, */  /*   empty, or longer than 0 characters */ void analyze_string( char * string ) {    assert( string != NULL );        /* Cannot be NULL */    assert( *string != '\0' );       /* Cannot be empty */    assert( strlen( string ) > 2 );  /* Length must exceed 2 */ }   Output  Analyzing string 'abc' Analyzing string '(null)' Assertion failed: string != NULL, file assert.c, line 24  abnormal program termination   Error Handling Routines |  Process and Environment Control Routines  See Also   abort, raise, signal, _ASSERT, _ASSERTE, _DEBUG

user: user-0
content: 顶顶

user: user-0
content: 引用 1 楼 shenchenman 的回复:1 断言有中断的效果 2 这种退出方式指的是什么？断言不需要退出 3 return是退出函数，exit是退出进程  1、请问有中断的效果是神马意思？ 2、断言不是判断错误就马上退出吗？ 3、这个大概能理解，那abort呢？

user: user-2
content: 1 断言有中断的效果 2 这种退出方式指的是什么？断言不需要退出 3 return是退出函数，exit是退出进程