A few weeks ago I asked a question about pointers .
Now I have another one that I think involves the Heap and the Stack.
The code is the following:
int main()
{
int *x; int *y;
int w;
x = new int();
w = 85;
*x = w;
y = x;
w++;
cout << *y;
return 0;
}
And I imagined that the relationship between *y
, *x
and w
was like this diagram that I had made earlier:
However, in the last Console Out when I show on the screen what has what is indicated by y
( *y
) it comes out 85 and not 86 as I thought.
I guess it has to do with the sentence:
x = new int();
But where does it point x
? Shouldn't it change after?:
*x = w;
Are there more bonus exercises on similar problems?
Let's go step by step.
int *x; int *y;
It creates two pointers to integer (
int *
) and does not assign a value to them, so they point to an indeterminate place.int w;
Creates an integer (
int
) and does not assign a value to it, so it contains an indeterminate value.x = new int();
Creates a new integer pointer (
int *
) that points to an integer value (int
) in heap, this newly created integer has an indeterminate value. Stores the created pointer in pointer to integerx
, said pointer loses its previous value.w = 85;
Assigns the value
85
to the integer variablew
, said variable loses its previous value.*x = w;
Assigns the value contained in the variable
w
to the integer pointed to by the integer pointerx
. The pointerx
two steps ago was made to point to an integer in dynamic memory created bynew int()
, now said integer (pointed to byx
) copies the value ofw
, which will be85
.y = x;
Assigns the value contained in pointer to integer
x
to pointer to integery
. The pointer to integery
loses its previous value and now points to the same place thatx
will be a heap integer created bynew int()
three steps ago, that integer (pointed to by bothx
)y
contains85
.w++;
Increases the value contained in the variable
w
, this value is85
what will become86
.cout << *y;
It shows by console the value contained in the pointer to integer
y
, said pointer points to the same place thatx
will be an integer in dynamic memory created bynew int()
means of five steps ago and that contains85
.If we assume that the initial state of the program is as follows:
With the following statement:
You are allocating 4 bytes to store an integer... and the allocation is pointed to by the pointer
x
. The state of the application looks like this:The following instruction:
It stores in the variable
w
the value 85... without problems.We go to the following instruction:
Here you're not making it
x
point tow
, but instead copying the value stored inw
into memory pointed to byx
, which we remember is a random memory location set aside withnew
.We go to the following instruction:
Now you make the pointer
y
point to the same memory asx
... which is the memory reserved withnew
.We can continue:
You increment
w
... but no pointer is pointing to this memory location...And finally:
With this instruction you print the value stored in the memory reserved with
new
, which we remember corresponds to the initial value ofw
, that is, 85.To achieve the relationship that you indicate in the image, your program would have to be more like this: