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.
2 Answers
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.
7 Comments
UncleBens
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?Yacoby
@UncleBens Ak, you are right, however the
(void)0 is not required. The (x) is though.Steve Jessop
@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.bta
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.Johannes Schaub - litb
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). |
If you mean assert, then that should be controlled with the NDEBUG macro.
1 Comment
jbcoe
NDEBUG alters memory initialisation too though