What are the mechanisms behind this function?

c++, function

Solution

This is how it looks in assembler:

    int s= 1;
002044A8  mov         dword ptr [s],1  
    int &z=s;
002044AF  lea         eax,[s]  
002044B2  mov         dword ptr [z],eax  
    raw(s);
002044B5  lea         eax,[s]  
002044B8  push        eax  
002044B9  call        raw (020110Eh)  
002044BE  add         esp,4  
    raw(z);
002044C1  mov         eax,dword ptr [z]  
002044C4  push        eax  
002044C5  call        raw (020110Eh)  

LEA (in `lea eax,[s]`) means Load Effective Address so you can see how `z` effectively contains a pointer to location of `s`.

push instructions that prepare the arguments before the function call clearly show that you get (the same) pointer as an input in both cases.

This is non-optimized code.

Problem

I wrote a small program: ``` #include <iostream> using namespace std; int raw(int &x) { cout<<x<<endl; cout<<&x<<endl; } int main () { int s= 1; int &z=s; raw(s); raw(z); return 0; } ``` The output is(as expected): ``` 1 0x7fff5ed36894 1 0x7fff5ed36894 ``` It works as I expect it to be but I am curious about how this is implemented internally. Is it function overloading or something else or one of the function is a wrapper around the other function to provide user-friendliness or the compiler does casting while on its own?

Original source