#+title: Task 3: Array-based Vector, Rule of Three
#+author: JirR02

* Task

  You are provided a partial implementation of an array-based vector class =our_vector=. Declarations are given in file =our_vector.h=, member functions that are already implemented are in file =our_vector_locked.cpp=. Your task is to implement the copy constructor, assignment operator and destructor for class "our_vector", in file =our_vector.cpp=. Note that the vectors store values of type tracked and not =int= as in the lecture, but you can treat them in the same fashion; the tracked values are used behind the scenes for checking your solution.

  *Steps*:

  1. Implement the copy constructor for class =our_vector= so that it creates a copy of the internal array.

  1. Implement the assignment operator so that it creates a copy of the provided =our_vector= and destroys the vector that is currently assigned to the left-hand-side of the assignment (e.g., by swapping its current contents to a local copy, as described in the lecture).

  1. Implement the destructor so that it deallocates the internal array.

  *Memory tracking*: The internal elements of class =our_vector= are objects of class tracked (see file =tracker.h=). Such objects encapsulate a single integer location which is tracked by an internal memory manager. This is used internally to catch as many memory/deallocation errors as possible upon occurrence.

  *Testing*: Tests are already provided in file =main.cpp=. If you want to carry out further testing yourself, you may do so within function =your_own_tests()=, which is called by =main()= when encountering an unknown test identifier. You can edit this function in file =our_vector.cpp=.

  *Valgrind output* After you implemented the copy constructor (subtask 1), you may see the following (or similar) output generated by valgrind:

  #+BEGIN_src shell
  Remaining tracked elements: 6

  memory errors:
  [ERROR:Leak_DefinitelyLost] 20 bytes in 1 blocks are definitely lost in loss record 1 of 2
  in function `our_vector::our_vector(int)` at our_vector_locked.cpp:6
  in function `test_copy_constructor()` at main.cpp:9
  in function `main` at main.cpp:63
  [ERROR:Leak_DefinitelyLost] 20 bytes in 1 blocks are definitely lost in loss record 2 of 2
  in function `our_vector::our_vector(our_vector const&)` at our_vector.cpp:5
  in function `test_copy_constructor()` at main.cpp:13
  in function `main` at main.cpp:63
  #+END_src

  This indicates that in your program some memory has been dynamically allocated (in this case one =our_vector= object), but then this memory is definitely /lost/ when the main function returns (the memory can not be longer accessed).

  To solve this issue, you have to implement the object destructor (subtask 3).

* Solution

  #+BEGIN_src cpp
  #include "our_vector.h"
  #include <unordered_set>

  our_vector::our_vector(const our_vector &vec) /* TODO */ {
    count = vec.count;
    elements = new tracked[count];
    for (int i = 0; i < count; ++i) {
      elements[i] = vec.elements[i];
    }
  }

  our_vector &our_vector::operator=(const our_vector &t) {
    if (this != &t) {
      delete[] elements;

      count = t.count;
      elements = new tracked[count];

      for (int i = 0; i < count; ++i)
        elements[i] = t.elements[i];
    }
    return (*this);
  }

  our_vector::~our_vector() { delete[] elements; }

  void your_own_tests() {
    // Any extra test here.
  }
  #+END_src

  --------------

  Made by JirR02 in Switzerland 🇨🇭
