Duplication Of Side Effects - Red Hat ENTERPRISE LINUX 3 - USING CPP Using Instructions

Using cpp, the c preprocessor
Hide thumbs Also See for ENTERPRISE LINUX 3 - USING CPP:
Table of Contents

Advertisement

Chapter 3. Macros
if (*p != 0)
SKIP_SPACES (p, lim);
else ...
The presence of two statements--the compound statement and a null statement--in between the
condition and the
else
The definition of the macro
statement. Here is how:
while
#define SKIP_SPACES(p, limit)
do { char *lim = (limit);
while (p
if (*p++ != ' ') {
p--; break; }}}
while (0)
Now
SKIP_SPACES (p, lim);
do {...} while (0);
which is one statement. The loop executes exactly once; most compilers generate no extra code for it.

3.9.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:
makes invalid C code.
SKIP_SPACES
lim) {
expands into
, for "minimum", like this:
min
((X)
(Y) ? (X) : (Y))
(foo (z)) ? (x + y) : (foo (z)));
and
X
can be altered to solve this problem, using a
\
\
\
\
\
for
.
foo (z)
Y
is an unsafe macro.
min
in a way that computes the value of
min
do ...
might be called two
foo
only
foo (z)
29
if

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the ENTERPRISE LINUX 3 - USING CPP and is the answer not in the manual?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Enterprise linux 3

Table of Contents