Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline functions and their implementation

This should be easy points for those who answer. There is a logical answer to this question, but I wanted to ask just to verify.

My understanding of how program flow works is simple. A function and its associated instructions are located at some point in memory. This location in memory is the single location that is used to store such instructions. When called, the location of the first instruction of that function is stored in the program flow. This memory pointer instructs the CPU where to go in memory to find the instructions for the required function. After jumping to this location and executing the instructions, normal program flow is restored and the CPU jumps back to where the original address instruction was located to proceed with successive instructions.

It is my understanding that inline functions are pasted right into the locations in which they were called. So, when a source file is written and an inline function defined, there actually exist multiple locations in memory where this function's instruction set is located (namely exactly where it is called). So, that is to say there is not an source location in memory similar to that of a non-inline function?

Further, during the compiling process, does the compiler just paste the inline function exactly where it was called and remove/replace the arbitrary argument names of the function's definition with the parameters passed to it?

like image 821
sherrellbc Avatar asked Dec 16 '22 02:12

sherrellbc


2 Answers

It is my understanding that inline functions are pasted right into the locations in which they were called.

It depends. Even though a function is inline it may not actually be inlined by the compiler.

So, that is to say there is not an source location in memory similar to that of a non-inline function?

It depends how the function was inlined. If there were 4 calls to an inline function in a translation unit, the compiler might decide that inlining one of them is OK, and the remaining calls should use a regular function call. So in this case there's a "source" location and an inlined location.

Further, during the compiling process, does the compiler just paste the inline function exactly where it was called and remove/replace the arbitrary argument names of the function's definition with the parameters passed to it?

An inlined function must have the same meaning as a normal function call.

like image 138
ldav1s Avatar answered Jan 06 '23 09:01

ldav1s


It is almost as you are thinking. First of all - it depends on the language / compiler. In C and GCC or MSVCC if you tell that function is an "inline" function it does not mean it will be inlined! This depends on the compiler and its implemenetation and your inline keyword / pragma is only a hint.

Additional if function is marked as "inline" than it is pasted everywhere original calls were but ... it is not deleted if you are compiling a library (static or dynamic). But if compiler decides to inline it, it really pastes the code ant optimizes the call.

Btw. in Haskell (GHC) the "inline" pragma is not only a hint, but you can be sure, compiler will do the inline process. Personally I would love to have a C++ compiler which will really inline every function I tell him to :)

like image 44
Wojciech Danilo Avatar answered Jan 06 '23 11:01

Wojciech Danilo