C / C ++ - Pointers (Heap and Stack)

7

A few weeks ago I did a question about pointers .

Now I have another that I believe involves the Heap and the Stack.

The code is as follows:

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 I had done before:

However, in the last Console Out when I show by screen what has what is pointed out by y ( *y ) it comes out 85 and not 86 as I thought.

I imagine it has to do with the sentence:

x = new int();

But, where does x point? Should not I change after?:

*x = w;

Are there more extra exercises on similar problems?

    
asked by jqc 15.01.2018 в 15:23
source

2 answers

8

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;
}
    
answered by 15.01.2018 / 15:34
source
8
  

Where does x point?

Let's go step by step.

int *x; int *y;

Create two integer pointers ( int * ) and do not assign them value, so they point to an undetermined place.

int w;

Create an integer ( int ) and do not assign it a value, so it contains an indeterminate value.

x = new int();

Create a new pointer to integer ( int * ) that points to an integer value ( int ) in dynamic memory, this newly created integer has an undetermined value. Stores the pointer created in the pointer to% x , that pointer loses its previous value.

w = 85;

Assign the value 85 to the entire variable w , that variable loses its previous value.

*x = w;

Assigns the value contained in variable w to the integer pointed to by the pointer to% x . The pointer x two steps ago was made to point to an integer in dynamic memory created by new int() , now said integer (pointed by x ) copies the value of w , which will be 85 .

y = x;

Assigns the value contained in the pointer to integer x to the pointer to integer y . The pointer to% y loses its previous value and now points to the same site as x which will be an integer in dynamic memory created by new int() three steps ago, said integer (pointed by both x as per y ) contains 85 .

w++;

Increase the value contained in the variable w , that value is 85 so it will become 86 .

cout << *y;

Show by console the value contained in the pointer to% y , said pointer points to the same site as x which will be an integer in dynamic memory created by new int() five steps ago and containing 85 .

    
answered by 15.01.2018 в 15:44