C ++ make links take up memory

I read that a link is simply an alias of a variable existing in a symbol table. Consider the following code

int main()
{
    int y = 6;
    int &z = y;
    int k = 43;
    test(2,y,5,78);
    cout << &y << "\n";
    cout << &z << "\n";
    cout << &k << "\n";
}

void test(int a,int & x, int g, int h)
{
    cout << &a << "\n";
    cout << &x << "\n";
    cout << &g << "\n";
    cout << &h << "\n";
}

For output, I get

0039F740
0039F848
0039F748
0039F74C
0039F848
0039F848
0039F830

If the link does not take up memory on the stack, why is the memory offset. For instance. In the function test, the local variable a is at 0039F740, but g is at 0039F748. Shouldn't be on 0039F744?

Can someone please explain this in detail?

+4
source share
2 answers

Your function has four parameters.

Each parameter must be passed to a function.

The fact that one of the parameters is a link does not change this basic fact. The extra space that you see is the reference parameter of the function.

. , , ++, , , .

- . - . , . , , . , , , ( - ?)

, , ​​ static ( ), , ++ .

, . , , , .

: ++ , , , . ++ - , . ++ , , , . .

+1

, " - ". , , . , "- ", .

, , - . int& x, x *(pointer) &x (pointer). , ++ , .

, , ( " " ), . , . , :

int foo(int a, int b) { return a + b; }

x86_64 System V rdi a, b rsi, rax, .

0

Source: https://habr.com/ru/post/1654515/


All Articles