内联函数能有地址吗? [英] Do inline functions have addresses?

查看:613
本文介绍了内联函数能有地址吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在著作C ++程序设计语言的作者状态7.1.1节:

In section 7.1.1 of the book "The C++ Programming Language" the author states:

内联函数仍具有一个唯一的地址等做一个内联函数的静态变量

"inline function still has a unique address and so do the static variables of an inline function"

我很困惑。如果我有一个内联函数,那么它不能有地址。这是否会发生在C也?

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.

例如:

#include <stdio.h>

inline void f() {
    printf("hello\n");
}

int main() {
    f();
    void (*g)() = f;
    g();
}

以上code打印你好两次。

我的 GCC 编译器(使用 -O )发射code是这样的:

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

正如你所看到的,有先看跌期权()的调用再到 L__Z1fv()的调用(这是 F()的重整名称)。

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()).

这篇关于内联函数能有地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆