My lecturer has asked me that in class, and I was wondering why is it a macro instead of a function?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Because it should compiled in DEBUG mode and should not compiled in RELEASE mode.
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:
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.__FILE__
) and line number (through__LINE__
)assert
to be substituted for a valid expression which does nothing (i.e.((void)0)
) when building in release modeSome assertions can be expensive to call. You've just written a high performance matrix inversion routine, and you add a sanity check
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.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) section7.2
Diagnostics paragraph 2 says:Why does it require this, the rationale given in Rationale for International Standard—Programming Languages—C is:
which is not very informative, but we can see from other requirements why. Going back to section
7.2
paragraph 1 says: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 section7.2.1.1
The assert macro which says:where footnote
165
says: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 theassert.h
header from Standrd C library: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 section7.2.1.1
:and it says (emphasis mine):
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 plain0
would allow us to useassert
in an assignment and when used correctly would likely generate anexpression 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.