以下例子中,存储了整数123的记亿体空间不能被删除,因为地址丢失了。这些空间已无法再使用。
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 int *a = new int(123);
6 cout << *a << endl;
7 // We should write "delete a;" here
8 a = new int(456);
9 cout << *a << endl;
10 delete a;
11 return 0;
12 }
int* p=new int;
p=new int; //p指针修改,原来申请内存的地址没有记录下来,于是无法释放
1 void MyFunction(int nSize) 2 { 3 char* p= new char[nSize]; 4 if( !GetStringFrom( p, nSize ) ){ 5 MessageBox(“Error”); 6 return; 7 } 8 …//using the string pointed by p; 9 delete p; 10 }