assert(); what does it do?
In basic terms, it is a way of checking to make sure your data is what you expect it to be.
asserts that exist in your program''s source code are placed into the program''s executable code in debug builds and usually ignored in release builds (because they are extra run-time overhead).
Anyway, within the assert you place a logic statement, if its evaluates to true everything is great, if it evaluates to false, the program will generate a breakpoint/exception. Most often this is used to check to make sure a parameter passed into a method is what you expect -- its not set to null, it has some flag set, whatever.
As a quick example, suppose I had this in my code:
assert(someThing != NULL);
If I''m running in debug mode and the code hits that line, if someThing (some pointer) IS null, the program will break and let me know there''s a problem. If someThing is not NULL, then the program will keep running happily.
Keep in mind that asserts() aren''t meant for runtime error checking, like exceptions, they exist to help you catch ''this should never happen'' type bugs during the development process.
asserts that exist in your program''s source code are placed into the program''s executable code in debug builds and usually ignored in release builds (because they are extra run-time overhead).
Anyway, within the assert you place a logic statement, if its evaluates to true everything is great, if it evaluates to false, the program will generate a breakpoint/exception. Most often this is used to check to make sure a parameter passed into a method is what you expect -- its not set to null, it has some flag set, whatever.
As a quick example, suppose I had this in my code:
assert(someThing != NULL);
If I''m running in debug mode and the code hits that line, if someThing (some pointer) IS null, the program will break and let me know there''s a problem. If someThing is not NULL, then the program will keep running happily.
Keep in mind that asserts() aren''t meant for runtime error checking, like exceptions, they exist to help you catch ''this should never happen'' type bugs during the development process.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement