Reputation: 2959
I have a question regarding inline functions in C/C++ and parameters copying. Let's assume I want to run this function without inline specifier:
void my_func(int param)
{
my_inner_func(param);
}
param
will be first copied to my_func
and then again to my_inner_func
. If the function my_func
were inline
, would the compiler copy param
only once for the my_inner_func
parameter or twice anyway?
I would like to ask for explanation for both C and C++.
Upvotes: 3
Views: 553
Reputation: 12708
Well, in inline functions, the compiler has access to all the code involved to inline, so it can detect if you are actually modifying the parameters, so it can avoid copying if your function does not modify the parameters. Said this, what's the problem you are facing to?
Upvotes: 0
Reputation: 92391
If the functions are not inlined, on most architectures the cost of the function call itself will be significantly higher than copying an int
.
On systems where parameters are passed in registers, the register for passing single int
parameters will already hold the value when entering the first function and it can just pass it on to the next function without creating a new copy.
Don't sweat the small stuff!
Upvotes: 0
Reputation:
It depends. An inlined function (btw, it's best to let the compiler decide whether to do this or not) must expose the same observable behavior as the normal, not inlined version. So the easiest approach would be to copy the parameter twice. The compiler could be smart enough to optimize out the second copy if the inner function doesn't write to it.
Upvotes: 2
Reputation: 13085
The compiler can do what the heck it wants, as long as it follows the as if
rule.
The rule states that at any point you try and measure the state of the program, it gives the correct answer.
Where you are not measuring it, it can re-order operations.
When the compiler is optimizing, it would be able to remove the second copy of parameters for both inline and non- inline code, unless there was code which tried to spot this. Then it would have to produce code which met the as-if rule, and possibly increase the number of copies.
Upvotes: 4
Reputation: 16243
inline
it is only a "suggestion" to the compiler, that can or cannot inline
your function. Obviously can also choose to inline
a function you didn't explicitly declare inline
.
In other words, based on your compiler and your optimization settings, you should look at the generated assembler to have an answer.
Upvotes: 2
Reputation: 275966
Both C and C++ specify behavior of your program in terms of an abstract machine.
my_func
's int param
exists in this abstract machine, and it is distinct from the my_inner_func
's int param
. They have separate identities. If you take the address of one and the address of the other, they are guaranteed to compare not-equal.
But if you don't take the address of either, neither need have an address.
If all you do to an int
is assign or initialize it, then use it to assign or initialize another int
, and the compiler can prove there is no defined way to reach the intermediate int
through indirection (like a pointer), then the intermediate int
need not exist on the actual target machine.
Some compilers have problems with doing this at link-time. Others do not.
Some operations will block the existence of that int
from being elided. Others will not.
I see nothing in your example that would require that intermediate int
to exist on the target machine.
Upvotes: 7