cfunctionmacrosglibfunction-declaration

What's the significance of a C function declaration in parentheses apparently forever calling itself?


In gatomic.c of glib there are several function declarations that look like this:

gboolean
(g_atomic_int_compare_and_exchange_full) (gint *atomic,
                                          gint  oldval,
                                          gint  newval,
                                          gint *preval)
{
  return g_atomic_int_compare_and_exchange_full (atomic, oldval, newval, preval);
}

Can someone explain what this code exactly does? I'm confused by several things here:

  1. The function name g_atomic_int_compare_and_exchange_full is in parentheses. What's the significance of this?

  2. The function's body apparently consists of nothing but a call to the function itself so this will run forever and result in stack overflow (pun intended).

I can't make any sense of this function declaration at all. What's really going on here?


Solution

    1. The function name g_atomic_int_compare_and_exchange_full is in parentheses. What's the significance of this?

    Putting a function name in brackets avoids any macro expansion in case there is a function like macro with the same name.

    That means, g_atomic_int_compare_and_exchange_full(...) will use the macro while (g_atomic_int_compare_and_exchange_full)(...) will use the function.

    Why is this used? You cannot assign a macro to a function pointer. In that case you can provide the definition as you saw it and then you can use

    ptr = g_atomic_int_compare_and_exchange_full;
    

    to use the function instead of the macro.

    1. The function's body apparently consists of nothing but a call to the function itself so this will run forever and result in stack overflow (pun intended).

    If you look into the associated header gatomic.h you will see that such a macro is indeed defined. And in the function body, no brackets are used around the function name. That means, the macro is used and it is not an infinite recursion.