If we assume that the initial state of the program is as follows:
variable posición de memoria valor
w 0x1000 (aleatorio)
x 0x1004 (aleatorio)
y 0x1008 (aleatorio)
With the following instruction:
x = new int();
You are reserving 4 bytes to store an integer ... and the reservation is pointed by the pointer x . The status of the application is as follows:
variable posición de memoria valor
w 0x1000 (aleatorio)
x 0x1004 0xA000
y 0x1008 (aleatorio)
--- 0xA000 (aleatorio)
The following instruction:
w = 85;
Stores in variable w the value 85 ... no problems.
variable posición de memoria valor
w 0x1000 85
x 0x1004 0xA000
y 0x1008 (aleatorio)
--- 0xA000 (aleatorio)
We move on to the following instruction:
*x = w;
Here you are not making x point to w , but you are copying the value stored in w in the memory pointed to x , remember that it is a random position of reserved memory with new .
variable posición de memoria valor
w 0x1000 85
x 0x1004 0xA000
y 0x1008 (aleatorio)
--- 0xA000 85
We move on to the following instruction:
y = x;
Now you make the pointer y point to the same memory as x ... which is the reserved memory with new .
variable posición de memoria valor
w 0x1000 85
x 0x1004 0xA000
y 0x1008 0xA000
--- 0xA000 85
We can continue:
w++;
You increase w ... but no pointer is pointing to this memory position ...
variable posición de memoria valor
w 0x1000 86
x 0x1004 0xA000
y 0x1008 0xA000
--- 0xA000 85
And, finally:
cout << *y;
With this instruction you print the value stored in the memory reserved with new , which we remember corresponds to the initial value of w , that is, 85.
To get the relationship you indicate in the image, your program should look like this:
int main()
{
int *x; int *y;
int w;
// x = new int(); <<--- esto sobra
w = 85;
// *x = w; <<--- ya no se copia el valor de w
x = &w; // <<--- sino su dirección de memoria
y = x; // Ahora y apunta a w
w++;
cout << *y;
return 0;
}