Whole document tree
1.4.8.4 Duplication of Side EffectsMany C programs define a macro `min', for "minimum", like this:
When you use this macro with an argument containing a side effect, as shown here,
it expands as follows:
where `x + y' has been substituted for `X' and `foo (z)' for `Y'. The function `foo' is used only once in the statement as it appears in the program, but the expression `foo (z)' has been substituted twice into the macro expansion. As a result, `foo' might be called two times when the statement is executed. If it has side effects or if it takes a long time to compute, the results might not be what you intended. We say that `min' is an unsafe macro. The best solution to this problem is to define `min' in a way that computes the value of `foo (z)' only once. The C language offers no standard way to do this, but it can be done with GNU C extensions as follows:
If you do not wish to use GNU C extensions, the only solution is to be careful when using the macro `min'. For example, you can calculate the value of `foo (z)', save it in a variable, and use that variable in `min':
(where we assume that `foo' returns type `int').
This document was generated by root on January, 30 2002 using texi2html |