Question

Does C static inline function have identity at runtime?

Should I care about naming conflict of that constructs?

If the function is defined in .c file? Is it same?

Was it helpful?

Solution

Does C static inline function have identity at runtime?

Static inline function has identity within a compilation unit if the compiler at least once chooses not to inline it, or if you take the address of the function.

The taken address is valid only in the current compilation unit (.c file). In another compilation unit (another .c file), the compiler will give you a different address.

As with any other function with body visible to the compiler, the code from the function might be fully or partially replicated in various places in the assembly code generated by the compiler.

A static inline function (as any static function) is not visible from any other .c file than the currently compiled .c file.

Should I care about naming conflict of that constructs?

Only if you take the address of a static inline function defined in a header file and do the following:

  1. The static inline function f is defined in file a.h
  2. a.h is included by C files x.c and y.c
  3. x.c takes the address of f and stores it into a global variable f_addr
  4. y.c takes the address of f and compares it for identity to the value stored in f_addr
  5. The result of the comparison will the false, despite the fact that on a different level of abstraction it is the very same function f

If the function is defined in .c file? Is it same?

From C compiler viewpoint, it is exactly the same as defining the function in a header file and including the header file in the .c file. The compiler has no idea about header files, it only sees one continuous compilation unit. A header file is a concept that exists in the minds of programmers using the C language - this concept does not exist from the viewpoint of the C compiler.

OTHER TIPS

The naming of an inline function is just as important as the naming of a non-inlined function. The name is used by the compiler to identify which function to call. It is also used by programmers reading the code to understand what the function does.

At runtime the name of the function is not relevant.

Also note that the inline keyword is only a hint to the compiler. The function might not actually be inlined despite the inline keyword. Similarly, a function that is not marked as inline might still be inlined by the compiler.

You might not care about naming conflicts of inline functions, but I guarantee that the compiler will!

Inlining is a later-stage operation during compilation - the compiler will first need to determine the call structure of your program which requires the compiler to correctly resolve all named calls.

At runtime, you can't know the name of any currently executing function without explicitly coding some magic to determine it - c is not reflective.

As Mark points out, the inline keyword is only a hint - the compiler is free to ignore it if it decides better performance is gained from an explicit call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top