Take the 2-minute tour ×

I suspect some ASSERTION code is having side effects. I'd like to switch off ASSERT without making any other changes to how my code is compiled. I'm using MSVS2008. Switching from debug to release won't do as that will alter how memory is initialised.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Put this at the top of your header files after the inclusions of cassert (or a include that includes cassert)

#undef assert
#define assert(x) ((void)0)

Which redefines the assert marco so that it expands to nothing.

share|improve this answer
If I'm not mistaken, that turns off the assertion (program termination), but doesn't remove and stop the expression from being evaluated. Perhaps: #define assert(x) (void)0? –  UncleBens Feb 11 '10 at 17:23
@UncleBens Ak, you are right, however the (void)0 is not required. The (x) is though. –  Yacoby Feb 11 '10 at 17:36
@Yacoby: In general, you want it because having assert expand to a statement helps with warnings and such. For instance, if assert expands to nothing in release mode then if (foo) assert(bar); else assert(baz); might warn for empty if and/or else clauses. The latest draft of the C standard, at least, requires that the built-in assert is implemented that way. –  Steve Jessop Feb 11 '10 at 17:49
Turning off asserts using the #define above will only cause the statement inside the assert to be evaluated by the pre-processor. All #define statements will be evaluated and replaced by their appropriate values before the compiler runs, so this method should not cause the expression to be evaluated at run-time. –  bta Feb 11 '10 at 17:49
Not only will it avoid warnings, but will avoid errors. Consider: assert(baz()), assert(bar()); will fail in release mode if it simply expands to nothing. In C++ i would make it expand to (void()) or ((void)0) (the second has the advantage of working in C too). –  Johannes Schaub - litb Feb 11 '10 at 18:14
show 2 more comments

If you mean assert, then that should be controlled with the NDEBUG macro.

share|improve this answer
NDEBUG alters memory initialisation too though –  j coe Feb 11 '10 at 17:01
add comment

Your Answer


