9

for a lot of quick tasks where one could employ a function f(x,y), in plain C, macros are used. I would like to ask specifically about these cases, that are solvable by a function call (i.e. macros used for inlining functions, not for code expansion of arbitrary code).

Typically C functions are not inlined since they might be linked to from other C files. However, static C functions are only visible from within the C file they are defined in. Therefore they can be inlined by compilers. I have heard that a lot of macros should be replaced by turning them into static functions, because this produces safer code.

Are there cases where this is a not good idea?

Again: Not asking about Code-Production macros with ## alike constructs that cannot at all be expressed as a function.

7
  • 4
    I think your question might have been answered here stackoverflow.com/questions/9104568/macro-vs-function-in-c and here stackoverflow.com/questions/1358232/why-use-macros-in-c
    – Maru
    Oct 18, 2013 at 9:09
  • writing a proper macro is a pain, and inlineing functions just ensures it can be defined multiple times in the binary, the compiler can decide whether it will actually inline it to maybe save space Oct 18, 2013 at 9:20
  • 2
    I would actually suspect most of these are historical reasons. Ages ago, the C compilers didn't inline functions at all. So people used macros for things they wanted inlined. C code is usually unusually long-lived.
    – Jan Hudec
    Oct 18, 2013 at 10:59
  • @ratchetfreak: C does not have inline, this is not about C++.
    – wirrbel
    Oct 18, 2013 at 11:25
  • 2
    @wirrbel Of course there is inline in C, at least since C99. For a quick overview, see Wikipedia on inline functions.
    – amon
    Oct 18, 2013 at 14:12

1 Answer 1

6
+50

Macros that are solvable by a function call have a lot of pitfalls:

  • They are hard to write, because you may have to handle properly arguments like ++i.
  • They are hard to debug with a visual debugger since you cannot step through a macro or put a breakpoint there.
  • They are hard to handle correctly when analysing compilations dependencies.

Macros that are solvable by a function call could have been useful to provide inlining in a primitive compiler. I am not aware of any compiler not handling inline functions and some can even inline across compilation units.

Typically C functions are not inlined since they might be linked to from other C files.

There is no reason why a compiler would not be able to provide two versions of a function, a traditional called one and an inlined one. You should look at the documentation of the compiler you are targetting. Also, you may want to look at generated assembly: even if you do not know assembly, you can quickly learn to tell if some function has been inlined or not. (Start with baby-examples to quickly learn this.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.