Smart Pointers

PART A – Smart Pointers. 10 points

  • For each of the following statements, please:
    ? Explain the statement in 5 or more sentences. And
    ? Create a code experiment to demonstrate our understanding.
    ? Please remember to submit our code and to document our experiment in our assignment report.
  1. Deleting the same memory twice: This error can happen when two pointers address the same dynamically allocated object.
    If delete is applied to one of the pointers, then the object’s memory is returned to the Free-store. If we subsequently
    delete the second pointer, then the Free-store may be corrupted.
  2. Use smart pointers… Objects that must be allocated with new, but you like to have the same lifetime as other
    objects/variables on the Run-time stack. Objects assigned to smart pointers will be deleted when program exits that
    function or block.
  3. Use smart pointers… Data members of classes, so when an object is deleted all the owned data is deleted as well (without
    any special code in the destructor).
  4. Converting unique_ptr to shared_ptr is easy. Use unique_ptr first and covert unique_ptr to shared_ptr
    when needed.
  5. Use weak_ptr for shared_ptr like pointers that can dangle.
    PART B – Linked Bag. 30 points
  • Please change only files: LinkedBag340.cpp and Include.h, no other files.
  • We are to implement 8 small additional functions and 2 helper functions to the Linked Bag.
  • Our programs must produce identical output to the output in the 2 sample runs: Asmt03_Run1.txt and Asmt03_Run2.txt
    ? Our Test 9’s output must also be identical to the sample output excepts the random values.
    ? Our Test 9’s random values in our 2 sample runs’ output must be different.

Leave a Reply

Your email address will not be published. Required fields are marked *