Chapter 3. Macros
3.10.4. Duplication of Side Effects
Many C programs define a macro
#define min(X, Y)
When you use this macro with an argument containing a side effect, as shown here,
next = min (x + y, foo (z));
it expands as follows:
next = ((x + y)
where
has been substituted for
x + y
The function
is used only once in the statement as it appears in the program, but the expression
foo
has been substituted twice into the macro expansion. As a result,
foo (z)
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
The best solution to this problem is to define
once. The C language offers no standard way to do this, but it can be done with GNU extensions as
follows:
#define min(X, Y)
({ typeof (X) x_ = (X);
typeof (Y) y_ = (Y);
(x_
y_) ? x_ : y_; })
The
notation produces a compound statement that acts as an expression. Its value is the
({ ... })
value of its last statement. This permits us to define local variables and assign each argument to one.
The local variables have underscores after their names to reduce the risk of conflict with an identifier
of wider scope (it is impossible to avoid this entirely). Now each argument is evaluated exactly once.
If you do not wish to use GNU C extensions, the only solution is to be careful when using the macro
. For example, you can calculate the value of
min
in
:
min
#define min(X, Y)
...
{
int tem = foo (z);
next = min (x + y, tem);
}
(where we assume that
, for "minimum", like this:
min
((X)
(Y) ? (X) : (Y))
(foo (z)) ? (x + y) : (foo (z)));
and
X
((X)
(Y) ? (X) : (Y))
returns type
foo
int
for
.
foo (z)
Y
is an unsafe macro.
min
in a way that computes the value of
min
\
\
\
, save it in a variable, and use that variable
foo (z)
).
might be called two
foo
only
foo (z)
33
Need help?
Do you have a question about the ENTERPRISE LINUX 4 - USING CPP and is the answer not in the manual?
Questions and answers