HeLei Blog

深入理解c++引用

底层实现分析

  • 先看下面的一段代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    int main()
    {
    int x = 1;
    int &b = x;
    printf("&x=%x,&b=%x\n",&x,&b);
    printf("&x=%x,&x-1=%x\n",&x,&x-1);
    printf("&x=%x,&b=%x\n",&x,*(&x-1));
    return 0;
    }
  • 汇编后的代码

    1
    2
    3
    4
    5
    1 movl $1, -12(%rbp)
    2 leaq -12(%rbp), %rax
    3 movq %rax, -8(%rbp)
    4 movl $0, %eax
    5 popq %rbp
坚持原创技术分享,您的支持将鼓励我继续创作!