Do inline functions have addresses?
In section 7.1.1 of the book "The C++ Pr开发者_开发问答ogramming Language" the author states:
"inline function still has a unique address and so do the static variables of an inline function"
I am confused. If I have an inline function then it can't have address. Does this happen in C also?
The inline
attribute is just a hint to the compiler that it should try to inline your function. It's still possible to take the address of the function, and in that case the compiler will also need to emit a non-inline version.
For example:
#include <stdio.h>
inline void f() {
printf("hello\n");
}
int main() {
f();
void (*g)() = f;
g();
}
The above code prints hello
twice.
My gcc
compiler (with -O
) emits code something like this:
_main:
pushl %ebp
movl %esp, %ebp
pushl %ebx
subl $20, %esp
call ___i686.get_pc_thunk.bx
"L00000000002$pb":
leal LC0-"L00000000002$pb"(%ebx), %eax
movl %eax, (%esp)
call L_puts$stub ; inlined call to f()
call L__Z1fv$stub ; function pointer call to f() (g is optimised away)
movl $0, %eax
addl $20, %esp
popl %ebx
popl %ebp
ret
As you can see, there is first a call to puts()
and then a call to L__Z1fv()
(which is the mangled name of f()
).
Inline functions are have addresses if you need one. Standard only says that:
An inline function with external linkage shall have the same address in all translation units.
There is no contradiction. In parts where an inline function is called, its code can be inlined. In parts where you use a pointer to function, a non-inline version can be created to have an address.
The inline expansion of the function doesn't have an address, but if that function has a static variable, the variable does have an address. A static variable is basically just a global variable whose name is only visible locally (I.e., within the scope at which it is defined). Other variables in an inline function might be allocated on the stack (like they would be if it wasn't expanded inline) or they might just live in machine registers. The important part is that they are still separate variables, and have to act just like the function hadn't been expanded inline at all (unlike, for example, macros, where extreme care is needed to prevent multiple evaluations from causing problems).
They may be inlined at certain call sites but they still exist as normal function in address space.
I think you are confusing the location of the inlined functions object code, with the implications of inlining. Typically we visualize inlined functions as being placed within the calling function at the source code level. What the book is saying is that the variable names, including the use of static within inline functions, are treated just the same as if the function was in fact your typical, stand-alone function.
Furthermore, treatment of functions with the inline keyword, does not guarantee that they will be inlined, and for instances where they cannot (such as when requiring an address), a non-inlined version will be generated.
精彩评论