Link type and pointer when disassembling

Why are reference types and pointers the same in compiled code? (You can see in the third and fourth row). I tried to figure it out, but apparently I could not achieve.

If a variable of a reference type must be initialized in the declaration and cannot be changed, is there a need to take an indirect action, as in pointers?

int x = 10;

mov dword ptr [x], 0Ah

int y = x;

mov eax, dword ptr [x]

mov dword ptr [y], eax

int &i = y;

lea eax, [y]

mov dword ptr [i], eax

int *p = &x;

lea eax, [x]

mov dword ptr [p], eax

p = &i;

mov eax, dword ptr [i]

mov dword ptr [p], eax

x = i;

mov eax, dword ptr [i]

mov ecx, dword ptr [eax]

mov dword ptr [x], ecx

+3
source share
5 answers

, /, , , .

. , , , . - .

, , , - , .

, , - y i , V++ 2005 , i , y (.. i &i, y).

, , .

+2

- , . , . ++, :

int& r = *(reinterpret_cast<int*>(0x0));

, undefined!

, . , . , r- l-:

int x = 0;

int& r = x; // 1) no need to take the address of x like &x

r = r * x; // Manually: (*r) = (*r) * x

raw-. , , raw-. , .

, / , .

+2

. , non-reseatability.

, , ?

- , . : . , , , .

+1
+1

yep, references are just syntactic sugar for pointers (with restrictions suitable for the new syntax). It also allows C ++, apparently, to have parameters passed by reference, unlike C, which only has a copy by value (even pointer parameters)

0
source

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


All Articles