assert(); what does it do?

Started by
1 comment, last by Zerosignull 22 years, 8 months ago
what does assert(); do??? ~prevail by daring to fail~
Advertisement
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.

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.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911

This topic is closed to new replies.

Advertisement