迷途指针的问题?
以下有一程序:
1 typedef unsigned short int USHORT;
2 #include <iostream>
3
4 int main
5 {
6 USHORT *pInt=new USHORT;
7 *pInt=10;
8 std::cout<<"*pInt: "<<*pInt<<std::endl;
9 delete pInt;
10
11 long *pLong=new long;
12 *pLong=90000;
13 std::cout<<"*pLong: "<<*pLong<<std::endl;
14
15 *pInt=20;
16
17 std::cout<<"*pInt: "<<*pInt<<std::endl;
18 std::cout<<"*pLong: "<<*pLong<<std::endl;
19 delete pLong;
20 return 0;
21 }
输出:
*pInt: 10
*pLOng: 90000
*pInt: 20
*pLong: 65556
请问大家在第9行释放了pInt所指向的内存空间,但为什么在第11行将pInt原来内存地址赋给了指针pLong,这难道是巧合吗?那么这时候是不是pInt就指向了为long所分配的内存地址?为什么呢?他不是已经被释放了吗?按理说18行应该输出为20,但怎么变成了65556?