Question about Stray or Dangling Pointers
hello there,
take a look carefully on this code for example :
Code:
#include <iostream>using namespace std;
typedef unsigned short USHORT;
int main() {
USHORT * pInt = new USHORT;
*pInt = 20;
cout << "*pInt = " << *pInt << "\t&pInt = " << &pInt << endl;
delete pInt;
//pInt = 0;
long * pLong = new long;
*pLong = 90000;
cout << "*pLong = " << *pLong << "\t&pLong = " << &pLong << endl;
cout << "now assigning new value to *pInt\n";
*pInt = 10 ;
cout << "dont assigning value to *pInt\n";
cout << "*pLong = " << *pLong << "\t&pLong = " << &pLong << endl;
cout << "*pInt = " << *pInt << "\t&pInt = " << &pInt << endl;
return 0;
}
Output:
Code:
*pInt = 20 &pInt = 0x28ff1c
*pLong = 90000 &pLong = 0x28ff18
now assigning new value to *pInt
dont assigning value to *pInt
*pLong = 65546 &pLong = 0x28ff18
*pInt = 10 &pInt = 0x28ff1c
1. how come the *pLong value has been overwritten? it is not the same memory slot ?? (!!)
and if I compile and run the same code including line 11
The program crashes on line 16 when i reassign the value 10 to *pInt.
2. can someone help me understand why the program crashes after a assign NULL to that deleted pointer?