macO下C++ delete堆指针并不会真的删除指针的指向地址和改地址中保存的值,仅标记为空闲的内存。
void heapDemo() {
int *pointer = pointerfunc();
cout << pointer << endl;
cout << pointer << endl;
cout << pointer << endl;
delete pointer;
int *p2 = new int(11);
cout << "p2 = " << p2 << endl;
delete p2;
cout << "delete p2 = " << p2 << endl;
}
int * pointerfunc() {
//在堆区创建整形数据
//new返回的是该数据类型的指针
int * pointer = new int(11);
return pointer;
}
输出
0x10524b300
0x10524b300
0x10524b300
p2 = 0x10524b300
delete p2 = 0x10524b300
网友评论