Hello,
My question is this: if an inline function is expanded at the time of it's call, is a regular function allocated in memory allthe time?
I don't think I'm clearly understanding it. I have two different sources, here they are:
source1:
"...an inline function, is frequently used in class definitions. An inline function is a function that is expanded inline at the point at which it is invoked, instead of actually being called. There are two ways to create an inline function. The first is to use the inline modifier..."
"...The reason for inline functions is efficiency. Every time a function is called, a series of instructions must be executed, both to set up the function call, including pushing any arguments onto the stack, and to return from the function. In some cases, many CPU cycles are used to perform these procedures. However, when a function is expanded inline, no such overhead exists, and the overall speed of your program will increase. Even so, in cases where the inline function is large, the overall size of your program will also increase. For this reason, the best inline functions are those that are small. Most large functions should be left as normal functions..."
source2:
"...Tells the compiler to expand the function body at the point of the function call..."
What exactly does an inline function do and why is it so much better for smaller functions than larger ones?
If you have the same funtion, what difference does inline make?
Normally, for a non-inline function, the compiler puts the code in one place and calls it each time. For an inline function, the code is instead placed at the places it is called instead of generating a child function. So if you call the inline function from a hundred different places, the code that would have been compiled from the function had it not been inline is instead placed in the hundred different calling areas. This might result in slightly larger code but the overhead of calling a function is removed.
Alright, I think I understand now. The compiler plans to call an inline function but only knows how to call a normal function when it is needed? But wat about conditional statements? Does it just fill all the spaces up with inline functions to remove overhead?
The compiler takes the C++ code you write and converts it to machine code which the processor can execute.
When it converts a function call there are two options;
1) Put a copy of the (machine) code for the function where the call would occur
2) Put a jump to a (single) copy of the function elsewhere in the code.
This is all decided at compile time, and is then fixed in the exe.
An inline function is not called - a copy will exist at the place where you as a programmer have put the function call.
Another way of thinking of it.
A function that is not 'inline' only exists in one place in the executable. Each time other code calls that function, the execution jumps to where that function is, runs it, and then jumps back to where it was.
A function that is inline has multiple copies throughout the executable. The compiler replaces each call to the function with a full copy of the function code itself. This is more efficient because there is no 'jumping' but the multiple copies increase the size of your executable.