Homework 1: C/C++ Review

Due: Thursday, February 14, before 9:00 pm


Instructions: Your solutions to the following problems must be typed, converted to PDF, and submitted on Blackboard. It is okay to discuss concepts underlying the problems with other students, but all final solutions must be your own work.

All solutions must be typed, converted to PDF, and submitted on Blackboard. No other format is acceptable. Scans or photos of hand-written solutions will not be accepted.


Question 1: Pointers and Arrays

  1. Trace the values of m, n, and *p through lines 3 – 8 of the code; that is, show how each of the variables changes through each line of the code. What is the output? 1 int m = 5, n = 7 ; 2 int *p ; 3 p = &n ; 4 *p = 2 * n + m ; 5 *p = 2 * n + m ; 6 p = &m ; 7 *p = 2 * m + n ; 8 *p = 2 * m + n ; 9 cout << "m = " << m << ", " ; 10 cout << "n = " << n << ", " ; 11 cout << "*p = " << *p << endl ;
  2. Consider the following code fragment: int x ; int *ptr = &x ; ptr++ ; If the variable x is stored at address 3173315904, what is the value of ptr after execution of the code fragment?
  3. Suppose I want to make an array of Graph objects using the Graph class from Project 1. Why will the following not work? Graph *GArray = new Graph[100] ;
  4. What output does the following code fragment produce? int arr[5] = {1,2,3,4,5} ; int *ptr = arr ; cout << *(ptr + 2) << endl ;

Question 2: Classes

Consider the following code fragment:

class Animal { public: void Eats() { cout << "Eats food." << endl; } }; class Lion : public Animal { public: Lion() : Animal(), m_name("Leo") {} Lion(string name) : Animal(), m_name(name) {} void Eats() { cout << m_name << " eats meat." << endl; } void Sleep() { cout << "Ahh...a nice nap!" << endl; } private: string m_name; };
  1. Suppose leo is a Lion object. What syntax would I use to invoke the Eats() method on leo?
  2. Suppose lionPtr is a pointer to a Lion object. What syntax would I use to invoke the Sleep() method on the Lion object pointed to by lionPtr?
  3. Why is the private class variable called m_name and not simply name?
  4. What output will the following code fragment produce? Animal *leo = new Lion("Leo") ; leo->Eats() ;

Question 3: Iterators

The WideArray class implements a special array type for which each element consists of two 64-bit (long) quantities. That is, the first element of a WideArray is the first two elements of the underlying long array, the second element of the WideArray consists of the third and fourth words of the underlying array, etc.

class WideArray { public: WideArray(int n) { m_data = new long[2*n] ; m_size = n ; } // Other class methods class waIterator { public: waIterator(long *ptr = nullptr) { m_ptr = ptr; } bool operator!=(const waIterator &rhs) { return m_ptr != rhs.m_ptr ; } void operator++(int dummy); pair<long, long> operator*() { return pair<long,long>(*m_ptr, *(m_ptr+1)) ; } private: long *m_ptr; }; waIterator waBegin() { return waIterator(m_data) ; } waIterator waEnd(); private: int m_size; long *m_data; };
  1. Why are waBegin and waEnd defined as members of the WideArray class rather than of the waIterator class?
  2. Write the implementation of operator++.
  3. Write the implementation of waEnd.

Question 4: Memory Errors

  1. What is the likely results of running the following code fragment? Why? int *ptr = (int *) 0xfeedbeef ; *ptr = 0 ;
  2. Suppose a class uses a dynamically-allocated integer array, pointed to by an int* variable named data. The current capacity of the array is stored in the integer variable capacity. The following code fragment is meant to dyamically resize the array, doubling its capacity. What is the major memory error and how would you detect it? int *tmp = new int[2 * capacity] ; for (int i = 0; i < capacity; i++) { tmp[i] = data[i] ; } data = tmp ; capacity = 2 * capactiy ;