Memory Management in Classes

When a class owns dynamic data (part of the data stored in heap memory region), special handling is required to avoid memory problems.

Implicitly-declared Methods

Some methods can be used without explicit declarations. They are usually methods that are essential to the object including the default constructor, destructor, copy constructor, copy assignment operator overloading, etc.

Note

No dynamic instance variables in class means no special handling is required.

  • Default constructor

    • Must declare and define explicitly if other constructor is present

    • Will only trigger all class-type instance variables’ default constructors; primitive types and arrays are not initialized. Uninitialized primitive types should be avoided to prevent undefined behaviors.

  • Destructor

  • Copy constructor

  • Copy assignment operator overloading

Note

Any implicitly-declared method can only handle non-dynamic instance variables. They must be explicitly declared and defined when they are triggered in objects containing dynamic data.

 1// Operations you can perform without defining any methods
 2// works with classes containing no dynamic data
 3// all instance variables can be duplicated using simple assignment
 4class Example {
 5  string name;
 6  int age;
 7  double grades[4];
 8 public:
 9  string getName() {return name;}
10  void setName(string name) {this->name = name;}
11  int getAge() {return age;}
12  void setAge(int age) {this->age = age;}
13  double getGrade(int index) {return grades[index];}
14  void setGrade(int index, int value) {grades[index] = value;}
15};
16
17int main() {
18  // allowed without explicit declaration
19  Example exp;  // trigger default constructor
20  exp.setName("John");
21  exp.setAge(14);
22  Example exp1(exp);  // trigger copy constructor
23  Example exp2;
24  exp2 = exp;  // trigger copy assignment operator overload
25  // both exp1 and exp2 will have name as "John" and age as 14
26}

Dynamic data in class

  • pointer-typed instance variables

    • holding a single value/object or an array

    • allocated using new and data stored in heap

    • simple assignment will cause shallow copy

  • Potential memory problems

    • memory leak - fail to delete

      • bad or no explicit destructor

      • dynamic data passed around without correct handling

    • shallow copy - fail to copy the dynamic data in the heap memory

      • only the memory addresses (values of pointers) are copied by assignment

      • implicitly-declared methods will cause shallow copy

      • bad or no explicit copy constructor

      • bad or no explicit copy assignment operator overload

      • only happens when objects are copied or copy assigned

      • will consequently cause the destructor to delete a same memory block in heap multiple times

  • Rule of three

    • The big three methods needed for classes to handle dynamic data correctly

    • Destructor

      • Mandatory because it will always be triggered

      • When an object is destroyed

        • Local object going out of scope

        • Dynamic object being delete d

      • place to delete dynamic data

    • copy constructor

      • Optional not needed if never triggered

      • Explicit triggering MyArray obj2(obj1);

      • Implicit triggering MyArray obj2 = obj1;

      • Implicit triggering in parameter passing:

        1// declaration
        2void function(MyArray myObj);
        3
        4// call
        5MyArray myObj1;
        6function(myObj1);
        
      • Take a const reference parameter to avoid triggering itself

      • Perform deep copy here

    • copy assignment operator overload

      • optional not needed if never triggered

      • explicit triggering MyArray obj2; obj2 = obj1;

      • implicit triggering Easy to miss

        • parameter passed by value

        • returned value

      • must clean up old data

      • take a const reference parameter to avoid pass-by-value

      • perform deep copy here

      • return *this; to return a reference of its own class type MyArray &

    Note

    Even in classes with dynamic data, the implementation of the big three is not always all mandatory. Only the destructor will be always triggered. Thus, the copy constructor or the copy assignment operator overloading implementations can be omitted if they are never triggered.

 1class MyArray {
 2 private:
 3  int *arr;
 4  int size;
 5 public:
 6  MyArray();
 7  MyArray(int size);
 8  MyArray(const MyArray& other);
 9  MyArray & operator=(const MyArray& other);
10  ~MyArray();
11}
12
13MyArray::MyArray() {
14  size = 0;
15  arr = nullptr;
16}
17
18MyArray::MyArray(int size) {
19  this->size = size;
20  arr = new int[size];
21}
22
23MyArray::MyArray(const MyArray& other) {
24  // you can access private members of other directly in C++!
25  size = other.size;
26  arr = new int[size];
27  for (int i = 0; i < size; ++i)
28    arr[i] = other.arr[i];
29}
30
31MyArray& MyArray::operator=(const MyArray& other) {
32  if (this == &other)  // self-assignment check
33    return *this;
34  size = other.size;
35  // must release memory of the old dynamic data first!
36  delete [] arr;
37  arr = new int[size];
38  for (int i=0; i<size; ++i)
39    arr[i] = other.arr[i];
40  return *this;  // IMPORTANT! return the current object by reference
41}
42
43MyArray::~MyArray() {
44  delete [] arr;
45}
  • Rule of five (optional content)

    • Big three plus two more methods

      • Move constructor

      • Move assignment operator overloading

    • Not mandatory but will improve efficiency

    • New syntax since c++ 11 using && to refer to a rvalue reference

    • Mutable(non-const) parameter to allow modification

 1// the parameter is no longer 'const' because you will modify it
 2MyArray::MyArray(MyArray && other) {
 3  size = other.size;
 4  arr = other.arr;
 5  // must make the other object ready for destruction
 6  // without this step the destructor may try to delete the moved data
 7  other.arr = null;
 8  other.size = 0;  // this is not necessary unless your destructor rely on the size
 9}
10
11// the parameter is no longer 'const' because you will modify it
12MyArray & MyArray::operator=(MyArray && other) {
13  if (this == &other)  // self-assignment check
14    return *this;
15  // Swap both the size and arr with the other object
16  // the destructor of the other object will destroy the old data
17  swap(size, other.size);
18  swap(arr, other.arr);
19  return *this;
20}

Ownership of Dynamic Data

Note

This section is for extended reading. It is beneficial to projects and self-improving but not essential in exams.

A dynamic data can be created anywhere and passed around. As the ownership changed. The last owner should take the responsibility to release the memory.

#. Passing dynamic data created outside as a parameter. The recipient owns the data after.

 1class DataType {
 2  // ...
 3};
 4
 5class MyArray {
 6 private:
 7  DataType *myDynamicData;
 8 public:
 9  MyArray(DataType *initialData = nullptr) {
10    myDynamicData = initialData;
11  }
12  ~MyArray() {
13    // delete here although it is not new'ed in the class
14    delete myDynamicData;
15  }
16};
17
18int main() {
19  DataType *initData = new DataType();  // main function owns it
20  MyArray obj1(initData);  // obj1 owns it now
21
22  return 0;
23}

#. You may also choose to remove the destructor and let the main function to delete if you feel it make more sense that the recipients are only users of the dynamic data and the main function is still the owner. Sometimes the dynamic data is shared in this case.

 1class Configuration {
 2  // ...
 3};
 4
 5class MyArray {
 6 private:
 7  Configuration *config;
 8 public:
 9  MyArray(Configuration *config = nullptr) {
10    this->config = config;
11  }
12};
13
14int main() {
15
16  Configuration *config1 = new Configuration();  // main function owns it
17  MyArray obj1(config1);  // obj1 uses it
18  MyArray obj2(config1);  // obj2 uses it
19
20  delete config1;
21  return 0;
22}

The dynamic data can also be passed as a returned value.

Example: The object always owns the data. The recipient just used it.

 1class MyArray {
 2 private:
 3  int * array;  // always owned by the object
 4  int size;
 5 public:
 6  // ... other methods
 7  // array is new'ed in some methods
 8  ~MyArray() {delete [] array;}
 9  int *getArray() {return array;}  // expose to outside
10  int getSize() {return size;}
11};
12
13int main() {
14  // declare and initialize data in MyArray obj1;
15  int size = obj1.getSize();
16  int *arr = obj1.getArray();
17  for (int i = 0; i < size; ++i)
18    cout << arr[i] << " ";
19  return 0;
20}

Example: The ownership is not clear. You can either let the object or the main function to delete the dynamic data. Both are reasonable.

 1class MyArray {
 2 private:
 3  int * array;  // always owned by the object
 4  int size;
 5 public:
 6  // ... other methods
 7  // array is new'ed in some methods
 8  int *getPositiveValues();
 9  int getPositiveCount();
10};
11
12int * MyArray::getPositiveValues() {
13  int posCount = getPositiveCount();
14  int *arr = new int[posCount];  // dynamic data, not meant to be owned
15  int j = 0;
16  for (int i = 0; i < size; ++i)
17    if (array[i] > 0) {
18      arr[j] = array[i];
19      ++j;
20    }
21  return arr;
22}
23
24int MyArray::getPositiveCount() {
25  int count = 0;
26  for (int i = 0; i < size; ++i)
27    if (array[i] > 0) ++count;
28  return count;
29}
30
31int main() {
32  // declare and initialize data in MyArray obj1;
33  int *positiveArray = obj1.getPositiveValues();
34  int size = obj1.getPositiveCount();
35  for (int i = 0; i < size; ++i)
36    cout << positiveArray[i] << " ";
37  delete [] positiveArray;  // positiveArray owned by the main function
38  return 0;
39}