1

I'm trying to brush up my C++ pointer knowledge after years not coding using C++

I have a code like this

#include<iostream>

using namespace std;

void manipulate(int **p) {
    cout << "====\n";
    cout << "Process 2\n";
    cout << **p << '\n';
    cout << *p << '\n';
    cout << p << '\n';
    cout << "====\n";


    int a = *(new int(777));
    *p = &a;
    cout << "====\n";
    cout << "Process 3\n";
    cout << **p << '\n';
    cout << *p << '\n';
    cout << p << '\n';
    cout << "====\n";
}

int main() {
    int *p;
    int a = 999;

    p = &a;

    cout << "====\n";
    cout << "Process 1\n";
    cout << *p << '\n';
    cout << p << '\n';
    cout << "====\n";

    manipulate(&p);

    cout << "====\n";
    cout << *p << '\n';
    cout << p << '\n';
    cout << "Process 4\n";
    cout << "====\n";

    *p = 888;
    cout << "====\n";
    cout << "Process 5\n";
    cout << *p << '\n';
    cout << p << '\n';
    cout << "====\n";

    manipulate(&p);

    return 0;
}

The code prints on my terminal like this

====
Process 1
999
0x7ffee05ea7bc
====
====
Process 2
999
0x7ffee05ea7bc
0x7ffee05ea7c0
====
====
Process 3
777
0x7ffee05ea724
0x7ffee05ea7c0
====
====
1
0x7ffee05ea724
Process 4
====
====
Process 5
1
0x7ffee05ea724
====
====
Process 2
1
0x7ffee05ea724
0x7ffee05ea7c0
====
====
Process 3
777
0x7ffee05ea724
0x7ffee05ea7c0
====

My question is, on the process 4, why it printed 1 on cout << **p << endl; code? Is it supposed to print 777 instead? Where the value 1 comes from?

Same as process 5, I already assigned the value to *p = 888;, but the value printed as 1 in the terminal. I thought when you assign value using new int on process 2, the values will not be destroyed, even after the function calls ends.

Any helps is appreciated, thank you

papannn
  • 23
  • 8
  • 3
    `int a` is a local variable that dies by the end of `manipulate`. You get address of `a` and store it in `p`. Dereferencing it past `manipulate` call is UB. Did you mean to write `*p = new int(777);`? (this version also doesn't leak memory immediately) – Yksisarvinen Apr 30 '22 at 22:19
  • @Yksisarvinen Ah yes, It's fixed now, Thank you. May I know what is UB is? also, I thought when using new int, the addresses will be saved on heap memory, and the value will be not destroyed after the function ends. May I know how this could happens? – papannn Apr 30 '22 at 22:24
  • All of those `end`s should be `'\n'`. `'\n'` ends a line. This code doesn't need the extra stuff that `std::endl` does. – Pete Becker Apr 30 '22 at 22:26
  • Thanks alot for the feedback @PeteBecker, I've changed it – papannn Apr 30 '22 at 22:30
  • 2
    @papannn UB - [Undefined Behavior](https://en.cppreference.com/w/cpp/language/ub) - _"...there are no restrictions on the behavior of the program..."_ – Richard Critten Apr 30 '22 at 23:23
  • 1
    _I'm trying to brush up my C++ pointer knowledge_ That's probably not the most productive thing you could be doing. Much better to learn about the tools that the standard library provides, since these will save you a lot of hard work when you start writing programs that actually do something. They will also eliminate a whole class of bugs. Some good books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Paul Sanders Apr 30 '22 at 23:46
  • 1
    Understanding pointers in depth is useful. Even though the use of pointers in c++ is quite a lot less than in c. Understanding pointers helps prevent and/or more quickly debug reference bugs. And reference bugs are too common because people think of references as aliases for some other object. They are but if that object is moved, which happens easily when one has a reference to something in a container, you can have instant UB when something else is added/deleted. A reference is a kind of const pointer to an object with syntactic sugar (auto dereferenced and implicit address of when created). – doug May 01 '22 at 04:37
  • Thanks alot @RichardCritten for clarifying what UB is, have a good day! – papannn May 01 '22 at 10:54
  • Wow, thanks alot @PaulSanders for giving me the C++ reference. This will help me alot for learning C++, have a good day! – papannn May 01 '22 at 10:55
  • Thanks alot @doug for giving me a new insight, have a good day! – papannn May 01 '22 at 10:57
  • 1
    You are right, `new` allocates object on the heap and it will live outside of the function (until `delete`). But `int a` is a local variable, allocated on the stack. What you do is first create an object on the heap with `new int(777)` (this returns a pointer), then you dereference this pointer with `*(new int (777))` (this results in `int`) and then you construct object on stack `int a` and initialize it with your dereferenced pointer. You now have two `int`s, both equal to `777`, one living in the heap, and one on the stack. – Yksisarvinen May 01 '22 at 13:09
  • Thanks alot for clarifying @Yksisarvinen. Have a good day! – papannn May 01 '22 at 14:42
  • @doug OK, perhaps I should have said " That's probably not the most productive thing you could be doing at this stage", or something, especially when you look at the rabbit hole that the OP is heading down. I agree, the time for learning about pointers will come, but not yet. – Paul Sanders May 01 '22 at 18:38

0 Answers0