Pages

Monday, December 3, 2012

ISU - Exercise 9 - Resource Handling

Goals:

To understand the problems with memory leaks and how to prevent them, first by implementing it ourselfs and then by using smart pointer boost.

Exercise 1: Ensuring garbage collection on dynamically allocated std::string

Implement the following UML diagram:

Our code solution:


The output when using the test program provided in the exercise:


Questions to answer:
Why must the copy constructor and assignment operator be private with no implementa-
tion? and what is the consequence when these are private?

We make them private so it's impossible to create copies of the same object. If we allowed copies we would get multiple objects which points to the same stringPtr. When the destructor is called stringPtr is deleted and the program will crash because it no exists to the other objects.


What exactly does the operator->() do?
It fetches the object so you can write: *ss="Davs";

Exercise 2: The counted pointer

Extending the smartstring such the multiple parties can have a reference to it. We now implement the assignment and copy operator.

What happens in the destructor and how should it be implemented?
We check if count is zero. Only if count is zero will we delete that data.

New interface:










main.cpp:

smartstring.h:

Output:



Exercise 4: Discarding our solution in favor of boost::shared_ ptr<>

Instead of using our newly created solution and reinventing the wheel. We use boost::shared_ptr to give us the same functionality.

To use boost::shared_ptr we need to install a library:
sudo apt-get install libboost_dev

Exercise 4.1: Using boost::shared_ptr<>

Test program using boost::shared_ptr<> (forcing segmentation fault)
Output:





boost::shared_ptr<> works.We didn't simulate a cycle in this test but to prevent cycles we would use the weak ptr which doesn't add to the counter.

Exercise 5: Resource Handling

Perspective of this particular exercises:
What do you consider a resource?
CPU time
Memory
Files
Inputs and outputs

In which situations do you foresee challenges with resources and how could they be han-
dled?
In all non-trival C++ programs which requires resources in any way. When needing shared memory it is good to use these pratices because you allways know who is responsible of the resource

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)?
Embedded systems and systems with limited resources. It allows you to use the same memory several places.. Instead of copying data and do math on them you can point to them directly which reduces memory usage. 

1 comment:

  1. The important aspects of this exercise is to understand how to handle recourses in various scopes.
    First to understand a simple smart pointer ”smartstring” witch automatically deletes the pointer when it goes out of scope. It is important to understand here that there can be only one reference to the object.
    The second thing is to make an upgraded version witch can handle copy’es of the pointer and keep track of them so that the pointer only is deleted when there are no one that uses it anymore.
    The last thing is to understand and use the boost library. It contains a smart pointer that has the same abilities as the upgraded “homemade” version.

    Your solution shows a good understanding of the principals in the exercise. A minor misunderstanding is the reason why we overload the ->(). It is not to made to make you able to assign with the = operator. It is made to make you able the use the semantics of a pointer on the object.

    ReplyDelete