Author: Omid David Tabibi
Date: 14:02:20 07/07/03
Go up one level in this thread
On July 07, 2003 at 13:35:02, Dieter Buerssner wrote: >On July 07, 2003 at 10:34:53, Robert Hyatt wrote: > >>On July 06, 2003 at 14:05:03, Omid David Tabibi wrote: >> >>>On July 06, 2003 at 13:12:25, Robert Hyatt wrote: > >>>>#define abs(x) ((x<0) ? -x : x) > >>>What's the difference between the above macro and the function >>> >>>int simple_abs (int i) { >>> return (i >= 0) ? i : -i; >>>} >>> >>>when inlined? >> >>No difference that I know of, if you have inlining enabled. > >Why do you think this? See for example my answer to Omid's question. Some >problems with the macro can be fixed, by putting the argument x into >parentheses. > >#define abs(x) ((x)<0 ? -(x) : (x)) > >As you stated it, for example abs(a+b) will fail (-a+b is in general not >-(a+b)). The problem with the sideeffects cannot be fixed in Standard C. In many >cases, slowness, cannot be fixed; for example the compiler may not be able to >find out, that a function call has no sideeffect. > >abs(slow_func_with_no_side_effect(a)) > >Similar cases, when the argument includes (volatile) global vars. > >The function version has no problem. At least with slightly modern compilers, it >should be best to use the abs() provided by the compiler in most cases. The only >reason, that comes into mind, that C has this abs() at all, is that it may be >done fast by tricks (provided by the compiler), that are not available in pure C >code. > Just a thought (regardless of our discussion about abs), why do people still use huge macros instead of functions? (e.g., those macros you posted in the other thread) Using long macros instead of functions only makes the task more complicated and error prone. Why not to write them as functions? Even though the inline keyword is not defined in ANSI C, most compilers support it. But even besides that, why not to code in C++ (using the C syntax)? You can still write C code in C++, but also take advantage of more options available in C++ (e.g., inline, references, etc). That's pretty much how I'm coding these days. >Regards, >Dieter
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.