Archived

This topic is now archived and is closed to further replies.

Zerosignull

assert(); what does it do?

Recommended Posts

gmcbay    130
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.

Share this post


Link to post
Share on other sites
Julio    116
one more thing, when you run in release mode the compiler skips over the asserts so you don''t even have to take them out.



How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.

Share this post


Link to post
Share on other sites