动态分配内存
动态分配内存
// Heap
#include <iostream>
using namespace std;
int* intOnHeap();
void leak1();
void leak2();
int main()
{
int* pHeap = new int;
*pHeap = 10;
cout << "*pHeap: " << *pHeap << "\n\n";
int* pHeap2 = intOnHeap();
cout << "*pHeap2: " << *pHeap2 << "\n\n";
cout << "Freeing memory pointed to by pHeap.\n\n";
delete pHeap;
cout << "Freeing memory pointed to by pHeap2";
delete pHeap2;
// get rid of dangling pointers
pHeap = 0;
pHeap2 = 0;
return 0;
}
int* intOnHeap()
{
int* pTemp = new int(20);
return pTemp;
}
void leak1()
{
int* drip1 = new int(30);
}
void leak2()
{
int* drip2 = new int(50);
drip2 = new int(100);
delete drip2;
}