Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- // Heap
- // Demonstrates dynamically allocating memory
- #include <iostream>
- using namespace std;
- int* intOnHeap(); //returns an int on the heap
- void leak1(); //creates a memory leak
- void leak2(); //creates another memory leak
- int main()
- {
- int* pHeap = new int; //allocates memory on the heap and returns its address.
- *pHeap = 10; //Receive the value
- cout << "*pHeap: " << *pHeap << "\n\n"; //Debug the value if I use cout << pHeap it will debug the address
- 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.\n\n";
- delete pHeap2;
- //get rid of dangling pointers
- pHeap = 0;
- pHeap2 = 0;
- return 0;
- }
- int* intOnHeap()
- {
- int* pTemp = new int(20);
- return pTemp;
- }
- //Examples of how not allocate memory (causes leak)
- void leak1()
- {
- int* drip1 = new int(30);
- }
- void leak2()
- {
- int* drip2 = new int(50);
- drip2 = new int(100);
- delete drip2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement