The presence of two statementsthe compound statement

Info icon This preview shows pages 39–42. Sign up to view the full content.

View Full Document Right Arrow Icon
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 { char *lim = (limit); \ while (p < lim) { \ if (*p++ != ’ ’) { \ p--; break; }}} \
Image of page 39

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 3: Macros 36 while (0) Now SKIP_SPACES (p, lim); expands into do {...} while (0); which is one statement. The loop executes exactly once; most compilers generate no extra code for it. 3.10.4 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 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 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 ). 3.10.5 Self-Referential Macros A self-referential macro is one whose name appears in its definition. Recall that all macro definitions are rescanned for more macros to replace. If the self-reference were considered a use of the macro, it would produce an infinitely large expansion. To prevent this, the
Image of page 40
Chapter 3: Macros 37 self-reference is not considered a macro call. It is passed into the preprocessor output unchanged. 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 the computer runs out of memory.
Image of page 41

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 42
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern