home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 6
/
FreshFish_September1994.bin
/
gnu
/
info
/
cpp.info-2
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-09-02
|
40KB
|
728 lines
This is Info file cpp.info, produced by Makeinfo-1.54 from the input
file cpp.texi.
This file documents the GNU C Preprocessor.
Copyright 1987, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions.
File: cpp.info, Node: Macro Parentheses, Next: Swallow Semicolon, Prev: Misnesting, Up: Macro Pitfalls
Unintended Grouping of Arithmetic
.................................
You may have noticed that in most of the macro definition examples
shown above, each occurrence of a macro argument name had parentheses
around it. In addition, another pair of parentheses usually surround
the entire macro definition. Here is why it is best to write macros
that way.
Suppose you define a macro as follows,
#define ceil_div(x, y) (x + y - 1) / y
whose purpose is to divide, rounding up. (One use for this operation is
to compute how many `int' objects are needed to hold a certain number
of `char' objects.) Then suppose it is used as follows:
a = ceil_div (b & c, sizeof (int));
This expands into
a = (b & c + sizeof (int) - 1) / sizeof (int);
which does not do what is intended. The operator-precedence rules of C
make it equivalent to this:
a = (b & (c + sizeof (int) - 1)) / sizeof (int);
But what we want is this:
a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
Defining the macro as
#define ceil_div(x, y) ((x) + (y) - 1) / (y)
provides the desired result.
However, unintended grouping can result in another way. Consider
`sizeof ceil_div(1, 2)'. That has the appearance of a C expression
that would compute the size of the type of `ceil_div (1, 2)', but in
fact it means something very different. Here is what it expands to:
sizeof ((1) + (2) - 1) / (2)
This would take the size of an integer and divide it by two. The
precedence rules have put the division outside the `sizeof' when it was
intended to be inside.
Parentheses around the entire macro definition can prevent such
problems. Here, then, is the recommended way to define `ceil_div':
#define ceil_div(x, y) (((x) + (y) - 1) / (y))
File: cpp.info, Node: Swallow Semicolon, Next: Side Effects, Prev: Macro Parentheses, Up: Macro Pitfalls
Swallowing the Semicolon
........................
Often it is desirable to define a macro that expands into a compound
statement. Consider, for example, the following macro, that advances a
pointer (the argument `p' says where to find it) across whitespace
characters:
#define SKIP_SPACES (p, limit) \
{ register char *lim = (limit); \
while (p != lim) { \
if (*p++ != ' ') { \
p--; break; }}}
Here Backslash-Newline is used to split the macro definition, which must
be a single line, so that it resembles the way such C code would be
laid out if not part of a macro definition.
A call to this macro might be `SKIP_SPACES (p, lim)'. Strictly
speaking, the call expands to a compound statement, which is a complete
statement with no need for a semicolon to end it. But it looks like a
function call. So it minimizes confusion if you can use it like a
function call, writing a semicolon afterward, as in `SKIP_SPACES (p,
lim);'
But this can cause trouble before `else' statements, because the
semicolon is actually a null statement. Suppose you write
if (*p != 0)
SKIP_SPACES (p, lim);
else ...
The presence of two statements--the compound statement and a null
statement--in between the `if' condition and the `else' makes invalid C
code.
The definition of the macro `SKIP_SPACES' can be altered to solve
this problem, using a `do ... while' statement. Here is how:
#define SKIP_SPACES (p, limit) \
do { register char *lim = (limit); \
while (p != lim) { \
if (*p++ != ' ') { \
p--; break; }}} \
while (0)
Now `SKIP_SPACES (p, lim);' expands into
do {...} while (0);
which is one statement.
File: cpp.info, Node: Side Effects, Next: Self-Reference, Prev: Swallow Semicolon, Up: Macro Pitfalls
Duplication of Side Effects
...........................
Many C programs define a macro `min', for "minimum", like this:
#define min(X, Y) ((X) < (Y) ? (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) < (foo (z)) ? (x + y) : (foo (z)));
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:
#define min(X, Y) \
({ typeof (X) __x = (X), __y = (Y); \
(__x < __y) ? __x : __y; })
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':
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
...
{
int tem = foo (z);
next = min (x + y, tem);
}
(where we assume that `foo' returns type `int').
File: cpp.info, Node: Self-Reference, Next: Argument Prescan, Prev: Side Effects, Up: Macro Pitfalls
Self-Referential Macros
.......................
A "self-referential" macro is one whose name appears in its
definition. A special feature of ANSI Standard C is that the
self-reference is not considered a macro call. It is passed into the
preprocessor output unchanged.
Let's consider an example:
#define foo (4 + foo)
where `foo' is also a variable in your program.
Following the ordinary rules, each reference to `foo' will expand
into `(4 + foo)'; then this will be rescanned and will expand into `(4
+ (4 + foo))'; and so on until it causes a fatal error (memory full) in
the preprocessor.
However, the special rule about self-reference cuts this process
short after one step, at `(4 + foo)'. Therefore, this macro definition
has the possibly useful effect of causing the program to add 4 to the
value of `foo' wherever `foo' is referred to.
In most cases, it is a bad idea to take advantage of this feature. A
person reading the program who sees that `foo' is a variable will not
expect that it is a macro as well. The reader will come across the
identifier `foo' in the program and think its value should be that of
the variable `foo', whereas in fact the value is four greater.
The special rule for self-reference applies also to "indirect"
self-reference. This is the case where a macro X expands to use a
macro `y', and the expansion of `y' refers to the macro `x'. The
resulting reference to `x' comes indirectly from the expansion of `x',
so it is a self-reference and is not further expanded. Thus, after
#define x (4 + y)
#define y (2 * x)
`x' would expand into `(4 + (2 * x))'. Clear?
But suppose `y' is used elsewhere, not from the definition of `x'.
Then the use of `x' in the expansion of `y' is not a self-reference
because `x' is not "in progress". So it does expand. However, the
expansion of `x' contains a reference to `y', and