Install/setup Dr.Memory (already done in lab).

Start with the MemoryTester project. Try each of the following - after making each change, rebuild the code and run with DrMemory to look for errors.

Dynamic Memory Basics

Example of dynamic (heap based)int:

int* p = new int(5);//allocate memory

Example of deleting dynamic int:

delete p;

Try each of the following - for each one, what does Dr.Memory report? What is it trying to point out?

  1. Make a dynamic int (don't delete it).
  2. Make a dynamic int, delete it, then try to print the value of it points to (cout < *p).
  3. Make a dynamic int, delete it twice.
  4. Make an int x (normal, stack based variable), set p to point to it, delete p.

Dynamic Memory With Arrays

Example of dynamic array of ints

int* p = new int[10];

Example of deleting dynamic array of ints:

delete[] p;

Try each of the following - for each one, what does Dr.Memory report? What is it trying to point out?

  1. Make a dynamic array of 10 ints, do not delete it. How much memory is 10 ints?
  2. Make a dynamic array of 10 ints, delete it using delete (no [] ). What does Dr. Memory think?
  3. Make a dynamic array of 10 ints, delete it using the delete[]. What does Dr. Memory think?
  4. Make a dynamic array of 10 ints. Make a pointer to the third element of the array:
    int* p2 = &p[2];
    Delete the array using the delete[]. Print the value p2 is pointing to.:
    cout < *p2 < endl;
    What does Dr. Memory report?

Dynamic Memory WithObjects

Example of dynamic Point:

Point* pt = new Point(4, 5);

Example of deleting dynamic Point:

deletept;

In the main, try each of the following. Run each and analyze with Dr. Memory.

  1. Make a dynamic Point, don't delete it. How much memory is leaked?
  2. Make a dynamic Point, delete the point and confirm you aren’t leaking memory.
  3. Make a dynamic array of Points, do not delete it.
    Point* ptArray = new Point[5]; //relies on default constructor for points to set them up…
    This corresponds to the picture below.
    How much memory does an array of 5 of them take?
  4. Delete the array with delete[] and confirm you aren’t leaking memory.

Dynamic Array of Dynamic Objects

Example of a heap based array of ptrs to Point objects - makes null (address 0) pointers

Point**pList =new Point*[3] {nullptr};

  1. Make a dynamic array of 3 Point pointers initialized to all null pointers. The picture below shows what we want.
    Don't delete it.

    How much memory got leaked?
  2. Add code to make each element in the array point to a point on the heap. Example of making an anonymous Point on the heap and storing pointer into the array:
    pList[0] = new Point(1, 4); //create dynamic Point, store point into array
    The picture below shows the goal. Do not delete anything.

    How much memory got allocated and leaked?
  3. delete [] pList. How much memory is now leaked? Why?
  4. Before deleting pList, delete each element of the array (Example: "delete pList[0];"). Confirm that there are no memory leaks now.