Macro or Function?

I l @ ve RuBoard

Macro or Function?

Many tasks can be done by using a macro with arguments or by using a function. Which one should you use? There is no hard and fast rule, but here are some considerations.

Macros are somewhat trickier to use than regular functions because they can have odd side effects if you are unwary. Some compilers limit the macro definition to one line, and it is probably best to observe that limit even if your compiler does not.

The macro-versus-function choice represents a trade-off between time and space. A macro produces in-line code; that is, you get a statement in your program. If you use the macro 20 times, then you get 20 lines of code inserted into your program. If you use a function 20 times, you have just one copy of the function statements in your program, so less space is used. On the other hand, program control must shift to where the function is and then return to the calling program, and this takes longer than in-line code.

Macros have an advantage in that they don't worry about variable types. (This is because they deal with character strings, not with actual values.) Therefore, the SQUARE(x) macro can be used equally well with int or float .

Programmers typically use macros for simple functions such as the following:

 #define MAX(X,Y)    ((X) > (Y) ? (X) : (Y)) #define ABS(X)       ((X) < 0 ? -(X) : (X)) #define ISSIGN(X)   ((X) == '+'  (X) == '-' ? 1 : 0) 

(The last macro has the value 1 ”true ”if x is an algebraic sign character.)

Here are some points to note:

  • Remember that there are no spaces in the macro name , but spaces can appear in the replacement string. With K&R C, there can be no spaces in the argument list. The preprocessor thinks the macro ends at the first space, so anything after that space is lumped into the replacement string, as shown in Figure 16.2. ANSI C, however, does permit spaces in the argument list.

    Figure 16.2. Faulty spacing in a macro definition.
    graphics/16fig02.jpg
  • Use parentheses around each argument and around the definition as a whole. This ensures that the enclosed terms are grouped properly in an expression such as forks = 2 * MAX(guests + 3, last) ;.

  • Use capital letters for macro function names . This convention is not as widespread as that of using capitals for macro constants. However, one good reason for using capitals is to remind yourself to be alert to possible macro side effects.

  • If you intend to use a macro instead of a function primarily to speed up a program, first try to determine if it is likely to make a significant difference. A macro that is used once in a program probably won't make any noticeable improvement in running time. A macro inside a nested loop is a much better candidate for speed improvements. Many systems offer program profilers to help you pin down where a program spends the most time.

Suppose you have developed some macro functions you like. Do you have to retype them each time you write a new program? Not if you remember the #include directive, reviewed in the following section.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net