Thursday, June 16, 2011

1.  <assert.h>  Diagnostics

The assert macro is used to add diagnostics to programs:
void assert(int expression)
If expression is zero (false) when assert(expression) is executed, the assert macro will print on stderr a message, such as
Assertion failed: expression, file filename, line nnn
It then calls abort (in <stdlib.h>) to terminate execution. The source filename and line number come from the preprocessor macros __FILE__ and __LINE__.
If NDEBUG is defined at the time <assert.h> is included, the assert macro is ignored.

EXAMPLE CODES

1.       assert.h
l assert.c
#include <stdio.h>
#include <assert.h>
void out(int *p) {
    assert(p != NULL);
    printf("%d\n", *p);
}
void
 main() {
    
int
 n = 10;
    out(&n);
    out(NULL);
}
>   cl assert.c && assert
10
Assertion failed: p != NULL, file assert.c, line 4

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
>   cl -D "NDEBUG" assert.c && assert
10
n VC IDE: Project Properties 참고










n VC IDE: Debug Release Configuration에서 각각 Build 실행

No comments: