C++代写:COMP301 Smart Pointers

实现智能指针Smart pointer, 包含一些C++高级用法,如Move constructor / Move assignment operator, 需要使用C++ 17标准。

Pointer

Requirement

In this assignment we implement our simplified versions ofsmart pointers. Smart pointers are included in STL (C++ standard template library) and implement some level of automatic garbage collection. Thus, smart pointers help to fight memory leaks. The idea is simple: make it so that once a block of dynamically allocated memory is no longer pointed by any smart pointer, it is automatically released/deleted.

You should submit me one or two files: my_memory.hand possiblymy_memory.cpp. my_memory.h contains the declarations of both classes.

my_unique_ptr

Class templat emy_unique_ptr is a pointer management class template. The object of this class points to the dynamically allocated memory. Only one object of the my_unique_ptr class can point to a given block of memory. Once the object of the my_unique_ptr class is destroyed it releases/deletes the block of memory to which it was pointing. We say that the my_unique_ptr object owns the memory.

The innards of the my_unique_ptr class are very simple: it contains a pointer to the datatype Type specified in the template (well, the actual unique_ptr also contains the deleter for the objects of Type but we don’t need it). Also, it contains the following public methods:

  • Default constructor that initializes the object to point to nullptr.
  • Constructor that takes a pointer Type * as a parameter and sets the object to point there. We say that the newly created object takes ownership of the pointed memory.
  • Move constructor that takes my_unique_ptr object and constructs a new object from it.
  • Copy constructor should be explicitly disabled/deleted !!!
  • Destructor that releases/deletes the block of memory pointed by the object.
  • Dereference operator * that returns a reference to the item pointed by the object.
  • Operator -> that returns the pointer to the object. It allows to use the my_unique_ptr object for class member access.
  • Move assignment operator.
  • Copy assignment operator must be explicitly disabled/deleted !!!
  • method isNullptr() that returns true if the pointer points to null ptr and false otherwise. This operator will help us to write tests.

my_shared_ptr

Class template my_shared_ptris a pointer management class template. The object of this class points to the dynamically allocated memory. Multiple my_shared_ptr objects can point to the same block of memory. When the my_shared_ptr object stops pointing to the block of memory it checks if any other my_shared_ptr object still points at it. If it finds that no other my_shared_ptr object points to that block of memory, it releases/deletes it.

Two main pieces of data that we need in the my_shared_ptr class are:

  1. pointer to the datatype Type stated in the template parameter.
  2. pointer to an integer counter, that keeps track of how many my_shared_ptr objects point to the same block of memory.

The following methods should be implemented:

  • Default constructor that sets all inner pointers to nullptr.
  • Constructor that takes Type * as a parameter and sets the object to point there. The object considers itself to be the first my_shared_ptr object to point to that block of memory. So the counter should be set to 1.
  • Copy constructor. Since the original object already points to the block of memory and now the newly created object points there too, you must increase the counter by 1.
  • Move constructor. Since the original rvalue wont point to the block of memory no more, the counter should remain the same.
  • Destructor that releases/deletes the block of memory and the counter if no other shared pointer points there.
  • Dereference operator * that returns a reference to the item pointed by the object.
  • Operator -> that returns the pointer to the object. It allows to use the my_shared_ptr object for class member access.
  • Copy and move assignment operators. If prior to the assignment the my_shared_ptr object was already pointing somewhere, the counter for the old block of memory should be decreased by 1, and if it reaches 0, the old block of memory should be released/deleted. Copy assignment should also increase the counter for the newly pointed block of memory.
  • method isNullptr() that returns true if the pointer points to null ptrand false otherwise. This operator will help us to write tests.

This assignment has a lot of places to mess up memory management. In situations like that, we need additional tools to help us out. One of the best tools here will bememory sanitizerand luckily we got one built in gcc compiler. We will discuss the idea in class but here is the short reminder on how to use it (main.cpp here is the driver with your library included in it)

g++ -std=c++17 -g -fsanitize=address,undefined main.cpp -o program_name

Also, I strongly recommend you to add-Walland maybe even-Wextraand inspect the resulting warning messages.