c++ – Why is the assembly code for reference and pointer the same?

Question:

C++:

    void f(int *ptr)
    {
       *ptr;
    }

    void f(int &ptr)
    {
       ptr;
    }

ASM:

f(int*):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        nop
        pop     rbp
        ret
f(int&):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        nop
        pop     rbp
        ret

Why is the assembly code for reference and pointer the same?

Answer:

Because the essence of a link, especially in such a simple case, is the same as that of a pointer. Roughly speaking , a link is a pointer that the compiler itself constantly dereferences for you.

Scroll to Top