Why is assert a macro and not a function?

CAssertC Preprocessor

C Problem Overview


My lecturer has asked me that in class, and I was wondering why is it a macro instead of a function?

C Solutions


Solution 1 - C

The simple explanation would be that the standard requires assert to be a macro, if we look at the draft C99 standard(as far as I can tell the sections are the same in draft C11 standard as well) section 7.2 Diagnostics paragraph 2 says:

> The assert macro shall be implemented as a macro, not as an actual > function. If the macro definition is suppressed in order to access an > actual function, the behavior is undefined.

Why does it require this, the rationale given in Rationale for International Standard—Programming Languages—C is:

>It can be difficult or impossible to make assert a true function, so it is restricted to macro form.

which is not very informative, but we can see from other requirements why. Going back to section 7.2 paragraph 1 says:

> [...]If NDEBUG is defined as a macro name at the point in the source file > where is included, the assert macro is defined simply as > > #define assert(ignore) ((void)0) > > The assert macro is redefined according to the current state of NDEBUG > each time that is included.

This is important since it allows us an easy way to turn off assertions in release mode where you may want to take the cost of potentially expensive checks.

and the second important requirement is that it is required to use the macros __FILE__, __LINE__ and __func__, which is covered in section 7.2.1.1 The assert macro which says:

> [...] the assert macro writes information about the particular call > that failed [...] the latter are respectively the values of the > preprocessing macros _FILE _ and _LINE _ and of the identifier > _func _) on the standard error stream in an implementation-defined format.165) It then calls the abort function.

where footnote 165 says:

>The message written might be of the form: > > Assertion failed: expression, function abc, file xyz, line nnn.

Having it as a macro allows the macros __FILE__ etc... to be evaluated in the proper location and as Joachim points out being a macro allows it to insert the original expression in the message it generates.

The draft C++ standard requires that the contents of the cassert header are the same as the assert.h header from Standrd C library:

> The contents are the same as the Standard C library header . >>See also: ISO C 7.2.

Why (void)0?

Why use (void)0 as opposed to some other expression that does nothing? We can come up with a few reasons, first this is how the assert synopsis looks in section 7.2.1.1:

void assert(scalar expression);

and it says (emphasis mine):

>The assert macro puts diagnostic tests into programs; it expands to a void expression.

the expression (void)0 is consistent with the need to end up with a void expression.

Assuming we did not have that requirement, other possible expressions could have undesirable effects such as allowing uses of assert in release mode that would not be allowed in debug mode for example using plain 0 would allow us to use assert in an assignment and when used correctly would likely generate an expression result unused warning. As for using a compound statement as a comment suggests, we can see from C multi-line macro: do/while(0) vs scope block that they an have undesirable effects in some cases.

Solution 2 - C

  1. It allows capturing the file (through __FILE__) and line number (through __LINE__)
  2. It allows the assert to be substituted for a valid expression which does nothing (i.e. ((void)0)) when building in release mode

Solution 3 - C

This macro is disabled if, at the moment of including , a macro with the name NDEBUG has already been defined. This allows for a coder to include as many assert calls as needed in a source code while debugging the program and then disable all of them for the production version by simply including a line like:

#define NDEBUG 

at the beginning of its code, before the inclusion of <assert.h>.

Therefore, this macro is designed to capture programming errors, not user or run-time errors, since it is generally disabled after a program exits its debugging phase.


Making it as function will increase some function calls and you can not control all such asserts in release mode.

If you use function then _FILE__, __LINE__ and __func__ will give the value of that assert function's code. Not that calling line or calling function's line.

Solution 4 - C

Some assertions can be expensive to call. You've just written a high performance matrix inversion routine, and you add a sanity check

assert(is_identity(matrix * inverse))

to the end. Well, your matrices are pretty big, and if assert is a function, it would take a lot of time to do the computation before passing it into assert. Time you really don't want to spend if you're not doing debugging.

Or maybe the assertion is relatively cheap, but it's contained in a very short function that will get called in an inner loop. Or other similar circumstances.

By making assert a macro instead, you can eliminate the calculation entirely when assertions are turned off.

Solution 5 - C

>Why is assert a macro and not a function?

Because it should compiled in DEBUG mode and should not compiled in RELEASE mode.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionItai BarView Question on Stackoverflow
Solution 1 - CShafik YaghmourView Answer on Stackoverflow
Solution 2 - CRobert Allan Hennigan LeahyView Answer on Stackoverflow
Solution 3 - CJeegar PatelView Answer on Stackoverflow
Solution 4 - Cuser1084944View Answer on Stackoverflow
Solution 5 - CAmir SaniyanView Answer on Stackoverflow