Le 11-04-2006 said:
" a.out: p113.c:8: main: Assertion `0' failed.
Aborted "
and a switch option NDEBUG, what other benefits does assert() provide
in any scope of designing, debugging/coding and/or testing?
Do you prefer the if statement of the language to the assert MACRO of
the precompiler?
To me, there are 3 differents uses of assert:
1) debuging
When debuging/developping, assert ensures that the logic of
your code is respected.
double get(Vector v, size_t index){
assert( index < size(v) ):
...
}
2) unitary tests
If you do not have a efficient framework for
unitary tests, assert is usefull.
void test_Vector(){
Vector v;
init(&v);
assert( size(v) == 0 );
add(&v, 1.0 );
assert( get(v,0) == 1.0 );
...
}
3) run-time error
The question of run-time errors is huge. In fact, assert
is only a mecanism, and your code should have a politic.
(Notice that bad user input or ressource lack is not a
"run time error").
It depends on your code: in embeded systems, sometime,
the better is to log the error and to reset. Then, assert
can be redefinied as log + reset. In an aplication done
for a home user with GUI, assert is not very user-friendly
solution. In a banking system, if a very strange thing occurs,
is it better to shut-down the system or to let the strange
thing occurs and to log it ? There is no general solution.
It depend.
One benefit of assert is that it is a macro. So, you should
associates differents behaviors to assert depending on
your politic.
At least, assert is often better than no error checking.
Its better to know that the code crashed with message
'assertion failed in toto.c:134' than it crashed when
the user sometime clicks on 'Save' button.
Marc Boyer