Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is assert a macro and not a function?

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

like image 621
Itai Bar Avatar asked Aug 13 '14 11:08

Itai Bar


People also ask

Is assert a macro?

The assert macro prints a diagnostic message when expression evaluates to false (0) and calls abort to stop program execution. No action is taken if expression is true (nonzero). The diagnostic message includes the failed expression, the name of the source file and line number where the assertion failed.

Is assert a function?

The assert() function tests the condition parameter. If it is false, it prints a message to standard error, using the string parameter to describe the failed condition.

What is assert macro in C?

C library macro - assert() The C library macro void assert(int expression) allows diagnostic information to be written to the standard error file. In other words, it can be used to add diagnostics in your C program.

What does assert () do in C++?

Answer: An assert in C++ is a predefined macro using which we can test certain assumptions that are set in the program. When the conditional expression in an assert statement is set to true, the program continues normally. But when the expression is false, an error message is issued and the program is terminated.

Is it better to use macros for assertions?

To avoid confusion in this article and in your code, it is advisable to use the name ASSERT rather than the assert that is defined in the standard library. There are two reasons to use macros for assertions, rather than a normal function call.

What is the use of assert () macro in JavaScript?

The assert () macro is used to check expressions that ought to be true as long as the program is running correctly. It is a convenient way to insert sanity checks. If you write a piece of code that computes the day of the month, then the following check may be useful:

What does it mean when assertions are removed from a macro?

When I say that the assertions are removed, I do not actually mean that the code is edited to remove them, but the macro is defined in such a way that the expression in the assert is never evaluated, saving processor cycles and code space, but, of course, losing the benefit of the check. When using the standard assert.

Why macros are no longer recommended?

The speed at which macros and functions differs. Macros are typically faster than functions as they don’t involve actual function call overhead. Conclusion: Macros are no longer recommended as they cause following issues. There is a better way in modern compilers that is inline functions and const variable. Below are disadvantages of macros:


Video Answer


2 Answers

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.

like image 132
Shafik Yaghmour Avatar answered Sep 17 '22 22:09

Shafik Yaghmour


  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
like image 33
Robert Allan Hennigan Leahy Avatar answered Sep 17 '22 22:09

Robert Allan Hennigan Leahy