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.

wirrbel
  • 3,028
  • 4
    I think your question might have been answered here http://stackoverflow.com/questions/9104568/macro-vs-function-in-c and here http://stackoverflow.com/questions/1358232/why-use-macros-in-c – Maru Oct 18 '13 at 09: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 – ratchet freak Oct 18 '13 at 09: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 '13 at 10:59
  • @ratchetfreak: C does not have inline, this is not about C++. – wirrbel Oct 18 '13 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 '13 at 14:12
  • @amon: oh, my bad. I have to stick to C90 though :( – wirrbel Oct 18 '13 at 14:30
  • C99 inline is different from C++ inline. You have to explicitly instantiate an inline function once and only once. – jdh8 Aug 20 '19 at 08:27

1 Answers1

6

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.)

user40989
  • 2,890