Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are parameters copied twice for inline in C?

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?

Edit

I would like to ask for explanation for both C and C++.

like image 900
Łukasz Przeniosło Avatar asked Jan 21 '26 11:01

Łukasz Przeniosło


1 Answers

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.

like image 93
Yakk - Adam Nevraumont Avatar answered Jan 24 '26 04:01

Yakk - Adam Nevraumont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!