#include <iostream>
using namespace std;
int * p1, * p2; // Global int pointers
void allocate() {
p1 = new int(88); // Allocate memory, initial content unknown
p2 = new int(99); // Allocate and initialize
}
int main() {
allocate();
cout << *p1 << endl; // 88
cout << *p2 << endl; // 99
delete p1;
delete p2;
cout << *p1 << endl;
cout << *p2 << endl;
return 0;
}
// output
p1 88
p2 99
after delete
p1 17962984
p2 99
If I change my function to below, it gives still the same result.
void allocate() {
p1 = new int;
*p1 = 88;
p2 = new int(99);
}
Can someone tell me why p2 is still giving 99 even after I delete it?
I am new to this concept, a good reference would be appreciated.