Exercise 1 - Ensuring garbage collection on dynamically allocated std::string
We have to implemented the SmartString class as follow:
We had a test program given from the start, but because of some problems with the << operator when using cout, we have changed a little on the code, as follow:
When we executes the program, we see the following.
We had a test program given from the start, but because of some problems with the << operator when using cout, we have changed a little on the code, as follow:
When we executes the program, we see the following.
Our SmartPointer class works!!
Questions to answer:
- Why must the copy constructor and assignment operator be private with no implementation? And what is the consequence when these are private?
In this program we do not use the copy constructor or the assignment operator, so we do not have to implement these. When these functions are under private, you can't copy the string.
- What exactly does the operator->() do?
The operator -> returns the pointer to the string.
Exercise 2 - The Counted Pointer
Things to be aware and consider:
- Why must the counter be dynamically allocated? Where should this happen?
It should happen in the constructor. It has to be dynamcally because it the can work as a pointer, that we can pass to the different instances of SmartString.
- How is the copy constructor implemented?
We put the str_ and the counter_ into the new object. After this, we increment the *counter_.
- How is the assignment operator implemented?
If the created object isn't exactly the same as the one we want to copy, we delete the str_ and counter_, and put the ones we want to copy into the new object. After this, we increment the *counter_.
- What happens in the destructor and how should it be implemented?
If there isn't any strings, the str_ and the counter_ have to be deleted.
We were given a new class of SmartString, and we have implemented it as follow:
When we executes our program we see the following:
We can now see, that our program and the SmartString works as intended. The string counter is incrementet when a new string is made, and when there is a end of scope the string counter is decrementet!!
Exercise 4 - Discarding our solution in favor of boost::shared ptr<>
Exercise 4.1 - Using boost::shared ptr<>
We now implement the boost::shared_ptr int the main.cpp file. This is instead of using the SmartString. The implementation is as follow:
When we executes the program we se the following:
We can see that the boost::shared_ptr works the same way as the class SmartString.
Exercise 5 - Resource Handling
Perspective of this particular exercises:- What do you consider a resource?
For this particular exercise, a resource is the dynamic allocated memory which could handle messages between threads.
- In which situations do you foresee challenges with resources and how could they be handled?
When handling with dynamic allocated memory you would want to release the memory when not in used any longer, there should be a proper garbage collection.
If you want to send messages between threads we can wrap the messages in a boost shared pointer. By doing so there will be a proper garbage collection which ensure us that we do not get memory leak.
For instance if you have three threads, A B and C, we want to send from A to C we ensure that messages is being sent before it gets destroyed. That is because they share a reference count which counts up for the amount of objects of that shared pointer. Deallocation of the memory only happens when reference count hits 0 and we are ensured that the objected pointed to by shared pointer is deleted.
If you want to send messages between threads we can wrap the messages in a boost shared pointer. By doing so there will be a proper garbage collection which ensure us that we do not get memory leak.
For instance if you have three threads, A B and C, we want to send from A to C we ensure that messages is being sent before it gets destroyed. That is because they share a reference count which counts up for the amount of objects of that shared pointer. Deallocation of the memory only happens when reference count hits 0 and we are ensured that the objected pointed to by shared pointer is deleted.
- In particular regarding memory, when would you be able eliminate the need for allocations? Or when is it a must that something is allocated (on heap)?
If we are on a system that has a very limited amount of memory, allocating on heap is more efficient as we can allocate/deallocate memory for other uses.
The important things in this exercise:
SvarSlet1) How to implement RAII in a class and to make sure it cleans up after itself when objects is not in use anymore.
2) To know when to increment or decrement the shared pointer.
3) Be able to use the boost shared pointer
Review:
You have implemented RAII and we can see it works. You can keep track of your counter and you delete objects when you don't use them anymore.
We can see that you have understanding for how boost shared pointer works and you have answered all the questions correctly.