VOID
OsrWaitForStop(POSR_DEVICE_EXT devExt)
{
devExt->HoldNewRequests = TRUE; ASSERT(devExt->State != STATE_STARTED); KeWaitForSingleObject(&devExt->StopEvent,
Executive,
KernelMode,
FALSE,
NULL);}
能给我解释一下这里的ASSERT实干什么用的吗?
OsrWaitForStop(POSR_DEVICE_EXT devExt)
{
devExt->HoldNewRequests = TRUE; ASSERT(devExt->State != STATE_STARTED); KeWaitForSingleObject(&devExt->StopEvent,
Executive,
KernelMode,
FALSE,
NULL);}
能给我解释一下这里的ASSERT实干什么用的吗?
int expression
);
Parameter
expression
Expression (including pointers) that evaluates to nonzero or 0.
Res
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 through 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 clicks 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 clicks Abort, the program aborts immediately. If the user clicks Retry, the debugger is called and the user can debug the program if just-in-time (JIT) debugging is enabled. If the user clicks Ignore, assert continues with its normal execution: creating the message box with the OK button. Note that clicking Ignore when an error condition exists can result in undefined behavior. For more information on CRT debugging, see CRT Debugging Techniques.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 expressions passed to them when the _DEBUG flag has been defined. Requirements
Routine Required header Compatibility
assert <assert.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP For additional compatibility information, see Compatibility in the Introduction.LibrariesAll versions of the C run-time libraries.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 24abnormal program termination
ASSERT()里面的必须为真,否则程序就会停下来。
"我断言,XXX是真地!“
在release模式下,这句话是不起作用的。
要注意的是有时使用ASSERT()时,会产生一些问题:
如:
ASSERT(i++>3);
上面语句在Debug模式 和 Release模式 会使程序有不同的结果。由于后者没有执行该语句,所以变量i还是ASSERT()前的值。前者则相反。
它完成对参数的判断,如果是TRUE则什么都不做,如果是FALSE则
弹出一个程序中断对话框提示程序出现错误。
在Release版本中它是什么作用都不起。它主要是监视程序在调试运行的过程中的运行情况,
多多使用它,绝对有好处,没有一点坏处。