6

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 2

7

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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 Ak, you are right, however the (void)0 is not required. The (x) is though.
@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.
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.
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).
|
6

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

1 Comment

NDEBUG alters memory initialisation too though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.