Jump to content
  • Advertisement


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


assert(); what does it do?

This topic is 6189 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!