0

I created a pointer code in which I played with pointers a little bit but when I tried to delete my pointer variable it still showed me the value which was stored in a pointer variable, after deletion I can still be able to use that pointer variable but I deleted that pointer variable so how can I reuse that after deleting? I don't get that.

Code:

#include<iostream>

int main()
{
    using namespace std;

    int *m = new int;
    int *ab = new int;

    cout << "Enter value of m: ";
    cin >> *m;

    cout << "Address of pointer m: " << m <<endl;
    cout << "Content of pointer m: " << *m << endl;

    ab = m;
    cout << "Address of pointer ab: " << ab <<endl;
    cout << "Content of pointer ab: " << *ab << endl;

    *m = 34;
    cout << "Address of pointer ab: " << ab <<endl;
    cout << "Content of pointer ab: " << *ab << endl;

    *ab = 7;
    cout << "Address of pointer m: " << m <<endl;
    cout << "Content of pointer m: " << *m << endl;

    delete ab;
    delete m;

    cout << *m + 1 << endl; //this is giving me result of *m + 1 which is not ok cuz I deleted pointer variable on above line.
    return 0;
}
  • 1
    not exactly a dupe because it's about scope but the principle about UB is the same: https://stackoverflow.com/a/6445794/2805305 – bolov May 20 '22 at 05:20
  • It's your responsibility as the programmer to make sure that you don't dereference invalid pointers in your programs. The compiler can't do it for you. Attempting to dereference an invalid pointer (which `m` is after `delete m`) leads to *undefined behavior*. – Some programmer dude May 20 '22 at 05:21
  • 1
    You also have a memory leak with `int *ab = new int; ab = m;`, followed by double deletion when you do `delete ab;` followed by `delete m;`. – Nathan Pierson May 20 '22 at 05:27
  • `delete ab` and `delete m` are not required to change the value of the pointers named `ab` and `m` respectively. They destroy the objects those pointers point to. Subsequently dereferencing those pointers by evaluating `*ab` or `*m` (which accesses the now-destroyed object) gives undefined behaviour. In your code, due to the assignment `ab = m`, the object originally pointed to by `ab` is leaked (never destroyed, and no pointers referring to it) and the object pointed to by `m` is released twice - which ALSO causes undefined behaviour. – Peter May 20 '22 at 05:27
  • @NathanPierson I created two pointer variables so I have to delete both of them to avoid memory leak – Mahad Ali May 20 '22 at 05:29
  • If you wanted to avoid the memory leak, you shouldn't have done `ab = m;` which overwrites the pointer that was previously stored in `ab`. Once you did that, you're no longer able to `delete` the pointer that was originally held there. – Nathan Pierson May 20 '22 at 05:30
  • @NathanPierson alright I understood that means I should not assign address of one pointer to another cuz that result in memory leak – Mahad Ali May 20 '22 at 05:35
  • It's not as simple as "assign address of one pointer to another results in a memory leak". There's plenty of pointer-to-pointer assignments that aren't memory leaks. But because this particular assignment erases the only way of accessing the address of an object with dynamic storage duration, that's a memory leak. For instance, the following wouldn't have been a memory leak: `int* foo = ab; foo = m; foo = nullptr;`, because the assignments to `foo` don't cause you to be unable to `delete` anything that you've previously `new`'d. – Nathan Pierson May 20 '22 at 05:38
  • @NathanPierson so should I prefer this syntax `int * foo = ab` or this one `int * foo = new int;` – Mahad Ali May 20 '22 at 05:58
  • Those are completely separate operations, not syntactically different ways of representing the same thing. Which one you prefer depends on what you actually want your program to do. – Nathan Pierson May 20 '22 at 05:59
  • @NathanPierson alright i understood a lil bit, thanks a lot – Mahad Ali May 20 '22 at 06:01

0 Answers0