Heap


The heap is memory set aside for dynamic allocation. Unlike the stack, you can allocate a block of memory at anytime using the "new" keyword. But once you have allocated memory on the heap, you are responsible for deallocating the moemory once you don't need it anymore by using the "delete" keyword. Failure to do so will result in a memory leak.

Pointers are used to access memory on the heap meaning that variables can be accessed globally.


Allocating space on heap



    
    
    

                    
0x0061ff0c
0x0061ff10 0x001d54f4 int *age;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack
Heap
0x001d54f0
0x001d54f4 18 memory leak!
0x001d54f8
0x001d54fc
0x001d5500


    
    
    

                    
0x0061ff0c
0x0061ff10 0x001d54f0 int *age;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack
Heap
0x001d54f0 23 memory leak!
0x001d54f4
0x001d54f8
0x001d54fc
0x001d5500


    
    
    

                    
0x0061ff04
0x0061ff08 0x001d54fc int *drivingAge;
0x0061ff0c
0x0061ff10 0x001d54f4 int *age;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack
Heap
0x001d54f0
0x001d54f4 14 memory leak!
0x001d54f8
0x001d54fc 16 memory leak!
0x001d5500

Delete ?



    
    
    

                    
0x0061ff0c
0x0061ff10 0x001d54f0 int *faveNum;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack
Heap
0x001d54f0 93 memory leak!
0x001d54f4 51 memory leak!
0x001d54f8
0x001d54fc
0x001d5500


    
    
    
    

                    
0x0061ff0c
0x0061ff10 0x001d54f8 int *faveNum;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack
Heap
0x001d54f0
0x001d54f4
0x001d54f8 100
0x001d54fc
0x001d5500


    
    
    
    
    

                    
0x0061ff0c
0x0061ff10 0x001d54f0 int *faveNum;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack
Heap
0x001d54f0 73
0x001d54f4
0x001d54f8
0x001d54fc 21 memory leak!
0x001d5500