tags:

views:

745

answers:

3

I came across this preprocessor definition while reading the source code in Windows Research Kernel (WRK) 1.2:

#define assert(exp) ((void) 0)

What does this code do? Why is it defined?

+14  A: 

It defines the expression assert(anything) to do nothing.

Presumably, the environment being used does not support the ANSI C assert statement, or the programmer was unaware of the fact that it could be disabled by defining NDEBUG.

bdonlan
Or the code was in assert.h
Steve Jessop
+4  A: 

To expand on what bdonlan says, the reason the macro does not expand empty is because if it did, then something like:

assert(something) // oops, missed the semi-colon
assert(another_thing);

would compile in release mode but not in debug mode. The reason it is ((void) 0) rather than just 0 is to prevent "statement with no effect" warnings (or whatever MSVC calls them).

Steve Jessop
A: 

Just to add, this is the definition of assert in newlib too, when NDEBUG is defined as a preprocessor directive. Newlib is the open source C library that is used on Cygwin and embedded systems.

From the assert manual in newlib:

The macro is defined to permit you to turn off all uses of assert at compile time by defining NDEBUG as a preprocessor variable. If you do this, the assert macro expands to (void(0))

Ashwin