5.1. Exercises#

Many of these exercises are taken from past exams of ECE 244 Programming Fundamentals courses at University of Toronto. The solutions are provided in the answer boxes.

Headings in this page classify the exercises into different categories: [Easy], [Intermediate], and [Challenging]. I suggest you start by easy exercises and work your way up to the challenging ones.

Question 3 in Fall 2022 Midterm Exam [Easy]

Consider the following C++ function:

void AvadaKedavra(int n) {
  int size = n + 1;
  int* q = NULL;
  for (int i = 0; i < 3; ++i) {
    q = new int[size];
  }
}

If somewhere in your main function you call AvadaKedavra(1). Based on the memory layout discussed during the lecture, answer this question: from the time this function starts to execute to the time right before it returns, how many bytes are newly allocated on the stack and the heap, respectively?

You may assume:

  1. all variables are put in the main memory.

  2. an int takes 4 bytes

  3. we have a 32-bit machine

Question 9 in Fall 2021 Midterm Exam [Intermediate]

Consider the code shown below. You can assume it compiles with no errors and runs.

#include <iostream>
using namespace std;

int a = 0;
int* b = &a;
int** c = &b;

int* foo(int** d) {
  (**d)++;
  b = *d;
  int* e = new int;
  *e = 10;
  return e;
}

int main() {
  int* g = nullptr;
  int* f = new int;
  *f = 5;
  a++;

  // Point 1

  g = foo(&f);
  a++;
  (*g)++;

  // Point 2

  return 0;
}

In the table below, give the values of the variables indicated in the table columns when program execution reaches each of the two points, Point 1 and Point 2. If a value cannot be obtained due to dereferencing a nullptr pointer, write nullptr (but assume the program does not stop).

a

*b

**c

*g

*f

Point 1

Point 2

Question 10 in Fall 2022 Midterm Exam [Challenging]

A Vtuber is an online entertainer who posts videos on Vtube. A Vtuber will have followers on Vtube. As a programmer from Vtube, you are asked to implement a class for Vtuber. The class definition and description are described below.

#include <string>
using namespace std;
class Follower {
 private:
  string name;
  int age;

 public:
  Follower(const string& _name, int _age) {
    name = name_;
    age = age_;
  }
  string get_name() const { return name; }
  int get_age() const { return age; }
};
class Vtuber {
 private:
  // Vtuber Name
  string name;
  // Follower array with a variable size, each element should be a dynamically
  // allocated object of class Follower.
  Follower** followers;
  // The size of follower array.
  int follower_max;
  // Number of followers
  int follower_num;

 public:
  Vtuber(const string& _name);
  ~Vtuber();
  void insert_follower(const string& follower_name, int follower_age);
  void remove_follower(const string& follower_name);
};

Specifically, Vtuber’s followers member variable is an array of pointers, each pointer pointing to a Follower object. The following graph illustrates it.

../../_images/followers.png
  1. Implement the constructor for Vtuber. Vtuber name should be initialized by _name, and follower_max should be initialized to 2. In addition, you should allocate an array called followers using new, with an initial size of 2 (the value of follower_max). Every element in this array should be a pointer to an object of class Follower and initialize all these pointers to NULL.

    Vtuber::Vtuber (const string& _name) {
    
  2. Every Vtuber in Vtube can get new followers or lose their current followers. This is implemented by two methods: insert_follower and remove_follower. Now you are asked to implement these two methods:

    1. For insert_follower, a follower name and follower age are given. You need to create a dynamically allocated object of Follower and insert it into the followers array (in the first available location), using new operator. If the array is full, you should double follower_max, allocate a new follower array, and move all the data into this new array, and insert the new follower. Write the code below.

      void Vtuber::insert_follower(const string& follower_name,
      int follower_age) {
      
    2. For remove_follower, a follower name is given. If there is any follower in the array matching the name, you should remove it and free its memory using delete. You can assume the follower names are all unique.

      void Vtuber::remove_follower(const string& follower_name) {
      
  3. Implement the destructor for the Vtuber class. You should free all the dynamically allocated objects using delete. Remember to be consistent with your previous implementation, as the entire program should not trigger any segmentation fault.

    Vtuber::~Vtuber() {