passing by reference

Discussion in 'C++' started by bob smith, Aug 23, 2013.

  1. bob smith

    bob smith Guest

    How does passing by reference work behind the scenes?

    Does the compiler secretly pass in a pointer to the object?

    Thanks.
    bob smith, Aug 23, 2013
    #1
    1. Advertising

  2. bob smith

    Luca Risolia Guest

    bob smith wrote:

    > How does passing by reference work behind the scenes?
    >
    > Does the compiler secretly pass in a pointer to the object?


    Yes. Have a look at the results of the compilation (without optimization) of
    the two programs below see that there is no real difference between them:

    // ref.cpp
    void f(int& a) {
    }

    int main() {
    int a;
    f(a);
    }

    // ptr.cpp
    void f(int* a) {
    }

    int main() {
    int a;
    f(&a);
    }

    $ gcc -O0 -S ref.cpp
    $ gcc -O0 -S ptr.cpp
    $ diff -u ref.s ptr.s

    --- ref.s 2013-08-23 21:30:03.507175417 +0200
    +++ ptr.s 2013-08-23 21:18:30.727740110 +0200
    @@ -1,8 +1,8 @@
    - .file "ref.cpp"
    + .file "ptr.cpp"
    .text
    - .globl _Z1fRi
    - .type _Z1fRi, @function
    -_Z1fRi:
    + .globl _Z1fPi
    + .type _Z1fPi, @function
    +_Z1fPi:
    .LFB0:
    .cfi_startproc
    pushq %rbp
    @@ -16,7 +16,7 @@
    ret
    .cfi_endproc
    .LFE0:
    - .size _Z1fRi, .-_Z1fRi
    + .size _Z1fPi, .-_Z1fPi
    .globl main
    .type main, @function
    main:
    @@ -30,7 +30,7 @@
    subq $16, %rsp
    leaq -4(%rbp), %rax
    movq %rax, %rdi
    - call _Z1fRi
    + call _Z1fPi
    movl $0, %eax
    leave
    .cfi_def_cfa 7, 8




    $ cat ptr.s
    .file "ptr.cpp"
    .text
    .globl _Z1fPi
    .type _Z1fPi, @function
    _Z1fPi:
    ..LFB0:
    .cfi_startproc
    pushq %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq %rsp, %rbp
    .cfi_def_cfa_register 6
    movq %rdi, -8(%rbp)
    popq %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
    ..LFE0:
    .size _Z1fPi, .-_Z1fPi
    .globl main
    .type main, @function
    main:
    ..LFB1:
    .cfi_startproc
    pushq %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq %rsp, %rbp
    .cfi_def_cfa_register 6
    subq $16, %rsp
    leaq -4(%rbp), %rax
    movq %rax, %rdi
    call _Z1fPi
    movl $0, %eax
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
    ..LFE1:
    .size main, .-main
    .ident "GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3"
    .section .note.GNU-stack,"",@progbits
    Luca Risolia, Aug 23, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    540
    E. Robert Tisdale
    Jul 16, 2003
  2. Victor Bazarov
    Replies:
    25
    Views:
    879
    E. Robert Tisdale
    Mar 23, 2005
  3. pereges
    Replies:
    7
    Views:
    436
    pereges
    Jun 1, 2008
  4. Ramon F Herrera
    Replies:
    8
    Views:
    478
    Pascal J. Bourguignon
    Sep 13, 2009
  5. Replies:
    2
    Views:
    145
Loading...

Share This Page