Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: All
Aug 20, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Assert() is Dangerous

The standard assert() macro tests its argument. If the result evaluates to 0, the standard abort() routine is called. Back in C heyday, it was a very useful debugging tool but it's less so in C++. However, if you are using it in C++ code, please mind the following:

1. Since assert() macro exists only when the DEBUG symbol is defined (otherwise assert() collapses to nothing), it should never be used to test runtime errors such as failed connection, file not found, invalid input and the like.

2. When an assertion fails, abort() is called. This is dangerous, since destructors of local objects are not invoked in this case. Throwing an exception, on the other hand, ensures that local objects' destructors are appropriately invoked.

Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date