Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if compiler inlines a function which is called through a function pointer

Let us say I have a function in my program and somewhere in my code, that function is called through a function pointer. What happens if the compiler happened to inline that function, or would the compiler realize that there is a function pointer assigned to that function and therefore avoid inlining it.

like image 375
MetallicPriest Avatar asked Feb 07 '13 10:02

MetallicPriest


People also ask

What is a pointer inline function?

inline is one of the misnomers of the C standard. Its main meaning is to be able to put the definition of a function in a header file without having to deal with "multiple definition" problems at link time.

What happens when you inline a function?

Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.

What happens when the keyword inline is placed in front of a function?

By doing inline, the function gets executed as inline, which removes the control transfer to that function's body.

Can a function pointer be used to call a function?

Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.


2 Answers

When a pointer to a function is taken, the compiler will generate an out-of-line body for the function. It is still possible to inline the function at other call sites.

Note that a function marked inline must have a definition available in all TUs which refer to it, and these definitions must be identical. Which means it's perfectly safe to inline the function at some call sites and keep it out-of-line at others.

like image 172
Angew is no longer proud of SO Avatar answered Oct 18 '22 19:10

Angew is no longer proud of SO


Well, it will surely work. I don't see how inlining would prevent that. You just have some code that calls the function directly, and it might be inlined there, and you have some code which calls it through a function pointer, just as a regular function.

like image 39
Luchian Grigore Avatar answered Oct 18 '22 19:10

Luchian Grigore