Topical Information

The purpose of this quiz is to give you a chance to focus your knowledge of dynamic memory in C++.

Quiz Information

Questions

  1. Dynamic memory is ____.

    1. allocated from the stack NO
    2. allocated from the heap YES
    3. allocated by the OS YES
    4. allocated by the compiler NO
    5. None of the above. NO
  2. What is nullptr and how is it used in relation to dynamic memory/pointers? (Hint: There are two ways!)

    
        nullptr is used to initialize a pointer to signal that it doesn't point
        to anything valid yet.  We also reinitialize a pointer to nullptr after
        we delete/[] it.
    
        nullptr is also returned by the nothrow version of new/[] when not
        enough memory can be gathered for the request.
    
    
  3. The ____ operator is used to allocate dynamic memory for a single object. The ____ operator, on the other hand, is used to allocate dynamic memory for an entire array of objects.

    1. nullptr, new NO
    2. *, delete NO
    3. *, & NO
    4. new, new [] YES
    5. new, [] NO
  4. Analogous to the allocation operators, there are deallocation operators which release dynamic memory we are no longer using. The delete operator will free space for a single dynamically allocated object. The delete[] operator will release memory for an entire dynamic array.

     

    No matter if it was an array or a single object, it is a good idea to assign the pointer a value of nullptr when done releasing its memory so that we won't accidentally attempt to access it again.

  5.  
    TRUE   FALSE   When using dynamic memory in a program that has classes, we might have dynamic memory inside the class or simply have dynamic memory elsewhere in the program.
    TRUE   FALSE   A common use outside the class is to have a dynamic array of class objects.
    TRUE   FALSE   A common use inside the class is to make an array member dynamic.
    TRUE   FALSE   Hmm...we use dynamic memory for arrays a lot, don't we?!
  6. If we make a class data member dynamic, we must do several things. We must have three methods in our class: copy constructor, destructor, and operator = (assignment). These help us to clean up class objects' dynamic memory when they leave scope (the destructor) and ensure that no two objects point to the same dynamic memory block (both copy constructor and assignment operator).

  7. Given the following class definition, show code for the default constructor, set_name method, and destroy method.

        class Person
        {
            char * name;
            size_t nameLen;
            // other data members
    
            void destroy(void);
    
        public:
            Person(void);
            Person(const char newName[]);
            Person(const Person & copy_me);
            ~Person(void);
    
            bool set_name(const char newName[]);
            // other methods
        };
    
    
        Person::Person(void)
            : name{nullptr}, nameLen{0} //, etc.
        {
        }
    
        bool Person::set_name(const char newName[])
        {
            delete [] name;
            nameLen = strlen(newName);
            name = new(nothrow) char[nameLen+1];
            if (name != nullptr)
            {
                strcpy(name, newName);
            }
            else
            {
                nameLen = 0;
            }
            return name != nullptr;
        }
    
        void Person::destroy(void)
        {
            delete [] name;
            name = nullptr;
            nameLen = 0;
            // other resets here?
            return;
        }
    
    
  8. Write two functions: allocate and deallocate. allocate should be able to create a pointer to either an array or a simple object depending on the caller's indication. (Hint: Think default arguments.)

    deallocate should be able to release dynamic memory for such an array/object (based on the same criterion as the allocation process) and clean up the pointer so that it won't be accidentally used again.

    As for the base types of all these pointers, please feel free to pick your favorite. *smile*

    
        inline double * allocate(double * & p, size_t len = 0)
        {
            return p = len > 1 ? new double[len] : new double;
        }
    
        inline void deallocate(double * & p, size_t len = 0)
        {
            if (len > 1)
            {
                delete [] p;
            }
            else
            {
                delete p;
            }
            p = nullptr;
            return;
        }
    
    
  9.  
    TRUE   FALSE   Dynamic memory must be checked to assure successful deallocation.
    TRUE   FALSE   Methods accessing a dynamic class member must use an if to be sure the member exists before dereferencing it.
    TRUE   FALSE   The ~ in a destructor's name has absolutely NOTHING to do with the ~ operator.
    TRUE   FALSE   As with constructors, destructors are only called explicitly by the application programmer.
  10. operator= always returns a reference to the calling object. It can use the implicit pointer argument this to do so. Besides this difference, the assignment operator and copy constructor differ in two other ways. Firstly, the assignment operator must insure that we aren't assigning an object to itself. This can be done by comparing our invisible pointer argument to the address of the argument object. Secondly, the assignment operator needs to clean up any old memory the object might have already been used. This isn't necessary for the copy constructor because the object never existed before.

     

    Both the = operator and copy constructor accept a const   reference to an object of the class in question. The assignment operator does this to avoid making too many copies of dynamic data. The copy constructor does this to avoid recursively calling itself.