-4

I am self learning C++ and I found my self on the Pointers section of C++.

To my understanding the pointer allocates the value of a variable to a memory.

But I came across on this problem which the answer is 588.

And I cannot figure out how this number came up.

Can someone please explain me step by step how 588 came up ?

Thanks in advance.


#include <iostream>

int main() {
    int *p, *q, *t;

    // Allocate memory and initialize pointers and values
    p = new int;
    q = new int;
    t = new int;
    *p = 17;
    *q = 7;
    *t = 42;

    p = t; // Make pointer p point to the same location as pointer t
    t = q; // Make pointer t point to the same location as pointer q

    *t = *p * *q;

    *q = *q + *t;

    std::cout << *t << std::endl;


    return 0;
}

Homer Jay Simpson
  • 1,043
  • 6
  • 19
  • 1
    Which line of c ode does something that you don't expect? Sprinkling `std::cout<<*p<<' '<<*q<<' '<<*t<<'\n';` after each line would explain everything, would it not? – Drew Dormann Aug 15 '23 at 19:47
  • @DrewDormann the part ` *q = *q + *t;` confuses me – Homer Jay Simpson Aug 15 '23 at 19:48
  • 1
    `t` and `q` are both pointing to the same `int`, which holds `294`. What's `294 + 294`? – Drew Dormann Aug 15 '23 at 19:49
  • @DrewDormann ` *t = *p * *q;` means 42*7=294 and because t and q pointing the same memory q becomes also 294.So ...ok – Homer Jay Simpson Aug 15 '23 at 19:50
  • 1
    There is no need to be sorry, but "I do not understand" is not a clear and focused question. It seems that you would benefit from a tutor or [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Drew Dormann Aug 15 '23 at 19:53
  • Pointers do not allocate memory. Pointers point to locations in memory. The operator `new` allocates memory. – Thomas Matthews Aug 15 '23 at 20:07
  • 3
    The big take-away from what you have learned in this question should be to inspect each step of each operation in the program either by printing out the variables used by that step or by observing them with [a debugger](https://en.wikipedia.org/wiki/Debugger). Often the debugger is the more efficient choice. – user4581301 Aug 15 '23 at 20:10

3 Answers3

2

You can easily get what is going on if you output all three pointers and theirs values after each change to them, or by using a debugger. This is what happens:

*p = 17;
*q = 7;
*t = 42;

// p points to an int with value 17
// q points to an int with value 7
// t points to an int with value 42

p = t; // Make pointer p point to the same location as pointer t

// p points to an int with value 42 (the same as t)
// q points to an int with value 7
// t points to an int with value 42 (the same as p)
    
t = q; // Make pointer t point to the same location as pointer q

// p points to an int with value 42 (now different than t)
// q points to an int with value 7 (the same as t)
// t points to an int with value 7 (the same as q)

*t = *p * *q;   // *t = (*p) * (*q) = 42 * 7 = 294

// p points to an int with value 42
// q points to an int with value 294 (still the same as t)
// t points to an int with value 294 (still the same as q)

*q = *q + *t;   // *q = (*q) + (*t) = 294 + 294 = 588

// p points to an int with value 42
// q points to an int with value 588 (still the same as t)
// t points to an int with value 588 (still the same as q)

std::cout << *t << std::endl;   // prints 588 (the same as q)

However note that something like p = t overwrites the address p was pointing to, therefore you cant free up the allocated memory anymore.

Joel
  • 721
  • 1
  • 13
1

There is written in the comments of the program

p = t; // Make pointer p point to the same location as pointer t
t = q; // Make pointer t point to the same location as pointer q

That is after these statements *p is equal to 42 and *t is equal to 7 the same way as *q is equal to 7 because now the both pointers t and q point to the same memory.

As a result you have that this statement

*t = *p * *q;

is euivalent to

*t = 42 * 7;

That is the object pointed to by the pointers t and q now contains 294.

This statement

*q = *q + *t;

that is the same as

*t = *q + *t;

because the both pointers t and q point to the same object and also is the same as

*t = *t + *t;

or

*q = *q + *q;

So 294 plust 294 yelds 588.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

When in doubt, you should draw it out! For example...

int *p, *q, *t;

You are declaring 3 pointers that don't point anywhere yet, thus:

p -> ?
q -> ?
t -> ?
p = new int;
q = new int;
t = new int;

You are allocating 3 integers (with indeterminate initial values), and making the 3 pointers point at them, thus:

p -> [ ... ]
q -> [ ... ]
t -> [ ... ]
*p = 17;
*q = 7;
*t = 42;

You are dereferencing the pointers and setting the values of the integers they are pointing at, thus:

p -> [ 17 ]
q -> [ 7  ]
t -> [ 42 ]
p = t;

You are making p point at the same integer that t is pointing at, thus:

p -+   [ 17 ]
   |
q -|-> [ 7  ]
   |
t -+-> [ 42 ]

t = q;

You are making t point at the same integer that q is pointing at, thus:

p -+     [ 17 ]
   |
q -|-+-> [ 7  ]
   | |
t -|-+
   |
   +---> [ 42 ]
*t = *p * *q;

You are multiplying the values of the integers that p and q are pointing at (42 * 7), and assigning the result (294) to the integer that t is pointing at, thus:

p -+     [ 17 ]
   |
q -|-+-> [ 294 ]
   | |
t -|-+
   |
   +---> [ 42 ]
*q = *q + *t;

You are adding the value of the integer that q and t are both pointing at (294 + 294), and assigning the result (588) to the integer that q is pointing at, thus:

p -+     [ 17 ]
   |
q -|-+-> [ 588 ]
   | |
t -|-+
   |
   +---> [ 42 ]
std::cout << *t << std::endl;

You are printing out the value of the integer that t is pointing at, thus:

588

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770