C++ Question
Does anyone know what the C++ keyword Assert does? I have seen it but never used it before
Thanks Alot!
~Steve~
The assert function (not a keyword) is a debugging tool. Use it like this:
This statement will cause the program to crash if its argumehnt evaluates to false; that is, if i is equal to -1. It is useful for sanity checks.
i = something();// i should never be -1 at this point, unless something has gone wrongassert(i != -1);// do other stuff
This statement will cause the program to crash if its argumehnt evaluates to false; that is, if i is equal to -1. It is useful for sanity checks.
.-Assert is not a keyword, is a macro.
.-Well assert() is a function, Assert() probably is a custom assert() function. It is defined in
.-assert(boolean expresion) evaluates that expresion and if false, stops the program showing a MessageBox. This works only in debug mode. It is used to track for logical errors.
.-You can also do : assert(a!=0 && "A is zero!"); . The MessageBox will show "A is zero!". Assert() is probably a wrapper that facilitates this, as well as using __FILE__ and __LINE__
.- Here is a simple Assert() macro:
#define Assert(EXP,TEXT) assert(EXP&&TEXT&&" Ocurred in file: "&&__FILE__&&", at line: "&&__LINE__)
PD

PD:There is one nice article about assert in GameProgramming Gems1...
What the hells!
It''s not a keyword, it''s a macro (sort of like a function). If the parameter you pass to it is true, assert does nothing. If the parameter is false, it halts your program and displays an error message. For example:
ASSERT(5 == 5); // this does nothing because it is true
ASSERT(5 == 10); // this halts the program because it''s false
The main use for this macro is to test if some variable has a usable value. Pointers need to be nonzero, and a size variable might have to be smaller than maxsize.
The really nice thing about this macro is that when you build your program in release mode, none of the assertion code gets put in the final executable. This means that the program you release to the public won''t have any of the speed decreases from the assertions in it.
ASSERT(5 == 5); // this does nothing because it is true
ASSERT(5 == 10); // this halts the program because it''s false
The main use for this macro is to test if some variable has a usable value. Pointers need to be nonzero, and a size variable might have to be smaller than maxsize.
The really nice thing about this macro is that when you build your program in release mode, none of the assertion code gets put in the final executable. This means that the program you release to the public won''t have any of the speed decreases from the assertions in it.
"It tastes like burning..."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement