For example, if you have a certain pointer that when you reach a given section of code should never be NULL - but either you're worried that you might have screwed up somewhere else so that it will end up NULL anyway, or someone else building on top of your code may accidentally do something to make it NULL, then you could have:
assert(thePtrThatShouldNeverBeNull != NULL);
This will ensure that if something goes wrong, you'll definitely know about it.
Also conveniently, after you're done debugging and you're confident that your code is 100% bug-free and you want a little extra performance, at the top of your code you can do:
#define NDEBUG
This will basically turn all the assert() statements off (become invisible to the compiler but not to you), so they won't affect performance in the final release of your program.
Author : Kevin Lam
0 nhận xét:
Post a Comment