Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!cbatt!ihnp4!cuae2!ltuxa!ttrdc!levy From: levy@ttrdc.UUCP Newsgroups: comp.lang.c Subject: Re: Disregarding parentheses in C Message-ID: <1670@ttrdc.UUCP> Date: Sat, 25-Apr-87 23:57:36 EDT Article-I.D.: ttrdc.1670 Posted: Sat Apr 25 23:57:36 1987 Date-Received: Thu, 30-Apr-87 05:30:09 EDT References: <7019@brl-adm.ARPA> <1504@umn-cs.UUCP> Organization: AT&T, Skokie, IL Lines: 22 Summary: WATCH OUT FOR SIDE EFFECTS AND FUNCTION CALLS In article <1504@umn-cs.UUCP>, herndon@umn-cs.UUCP (Robert Herndon) writes: < Often-times, I also use C as a "machine independent assembly < code". When I do so, I often define macros for performing < various operations, e.g., < #define ABS(x) ((x) < 0 ? -(x) : (x)) < and again, hope the C compiler doesn't literally evaluate x < repeatedly if it's a macro or expression that can be rearranged. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Just being able to be rearranged isn't enough! It can be perfectly rearrange- able and still have side effects. Note that if MACRO_1 is called multiple times by MACRO_2, and MACRO_1 contains something with side effects like x++ or getchar() then you had jolly well realize that the side-effects in MACRO_1 WILL get evaluated as many times as it was called, in an undefined order. Even if there are function calls in MACRO_1 which do not change the state of the computation, the compiler doesn't know to optimize the multiple calls to that function into one call, which is wasteful of CPU time. -- |------------dan levy------------| Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa, | an engihacker @ | vax135}!ttrdc!ttrda!levy | at&t computer systems division | Disclaimer: try datclaimer. |--------skokie, illinois--------|