1<html lang="en"> 2<head> 3<title>Duplication of Side Effects - The C Preprocessor</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="The C Preprocessor"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Macro-Pitfalls.html#Macro-Pitfalls" title="Macro Pitfalls"> 9<link rel="prev" href="Swallowing-the-Semicolon.html#Swallowing-the-Semicolon" title="Swallowing the Semicolon"> 10<link rel="next" href="Self_002dReferential-Macros.html#Self_002dReferential-Macros" title="Self-Referential Macros"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 152008, 2009, 2010, 2011 16Free Software Foundation, Inc. 17 18Permission is granted to copy, distribute and/or modify this document 19under the terms of the GNU Free Documentation License, Version 1.3 or 20any later version published by the Free Software Foundation. A copy of 21the license is included in the 22section entitled ``GNU Free Documentation License''. 23 24This manual contains no Invariant Sections. The Front-Cover Texts are 25(a) (see below), and the Back-Cover Texts are (b) (see below). 26 27(a) The FSF's Front-Cover Text is: 28 29 A GNU Manual 30 31(b) The FSF's Back-Cover Text is: 32 33 You have freedom to copy and modify this GNU Manual, like GNU 34 software. Copies published by the Free Software Foundation raise 35 funds for GNU development. 36--> 37<meta http-equiv="Content-Style-Type" content="text/css"> 38<style type="text/css"><!-- 39 pre.display { font-family:inherit } 40 pre.format { font-family:inherit } 41 pre.smalldisplay { font-family:inherit; font-size:smaller } 42 pre.smallformat { font-family:inherit; font-size:smaller } 43 pre.smallexample { font-size:smaller } 44 pre.smalllisp { font-size:smaller } 45 span.sc { font-variant:small-caps } 46 span.roman { font-family:serif; font-weight:normal; } 47 span.sansserif { font-family:sans-serif; font-weight:normal; } 48--></style> 49<link rel="stylesheet" type="text/css" href="../cs.css"> 50</head> 51<body> 52<div class="node"> 53<a name="Duplication-of-Side-Effects"></a> 54<p> 55Next: <a rel="next" accesskey="n" href="Self_002dReferential-Macros.html#Self_002dReferential-Macros">Self-Referential Macros</a>, 56Previous: <a rel="previous" accesskey="p" href="Swallowing-the-Semicolon.html#Swallowing-the-Semicolon">Swallowing the Semicolon</a>, 57Up: <a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro-Pitfalls">Macro Pitfalls</a> 58<hr> 59</div> 60 61<h4 class="subsection">3.10.4 Duplication of Side Effects</h4> 62 63<p><a name="index-side-effects-_0028in-macro-arguments_0029-76"></a><a name="index-unsafe-macros-77"></a>Many C programs define a macro <code>min</code>, for “minimum”, like this: 64 65<pre class="smallexample"> #define min(X, Y) ((X) < (Y) ? (X) : (Y)) 66</pre> 67 <p>When you use this macro with an argument containing a side effect, 68as shown here, 69 70<pre class="smallexample"> next = min (x + y, foo (z)); 71</pre> 72 <p class="noindent">it expands as follows: 73 74<pre class="smallexample"> next = ((x + y) < (foo (z)) ? (x + y) : (foo (z))); 75</pre> 76 <p class="noindent">where <code>x + y</code> has been substituted for <code>X</code> and <code>foo (z)</code> 77for <code>Y</code>. 78 79 <p>The function <code>foo</code> is used only once in the statement as it appears 80in the program, but the expression <code>foo (z)</code> has been substituted 81twice into the macro expansion. As a result, <code>foo</code> might be called 82two times when the statement is executed. If it has side effects or if 83it takes a long time to compute, the results might not be what you 84intended. We say that <code>min</code> is an <dfn>unsafe</dfn> macro. 85 86 <p>The best solution to this problem is to define <code>min</code> in a way that 87computes the value of <code>foo (z)</code> only once. The C language offers 88no standard way to do this, but it can be done with GNU extensions as 89follows: 90 91<pre class="smallexample"> #define min(X, Y) \ 92 ({ typeof (X) x_ = (X); \ 93 typeof (Y) y_ = (Y); \ 94 (x_ < y_) ? x_ : y_; }) 95</pre> 96 <p>The ‘<samp><span class="samp">({ ... })</span></samp>’ notation produces a compound statement that 97acts as an expression. Its value is the value of its last statement. 98This permits us to define local variables and assign each argument to 99one. The local variables have underscores after their names to reduce 100the risk of conflict with an identifier of wider scope (it is impossible 101to avoid this entirely). Now each argument is evaluated exactly once. 102 103 <p>If you do not wish to use GNU C extensions, the only solution is to be 104careful when <em>using</em> the macro <code>min</code>. For example, you can 105calculate the value of <code>foo (z)</code>, save it in a variable, and use 106that variable in <code>min</code>: 107 108<pre class="smallexample"> #define min(X, Y) ((X) < (Y) ? (X) : (Y)) 109 ... 110 { 111 int tem = foo (z); 112 next = min (x + y, tem); 113 } 114</pre> 115 <p class="noindent">(where we assume that <code>foo</code> returns type <code>int</code>). 116 117 </body></html> 118 119