Comp 1672, sections 1 and 2 Homework 7 Solutions 1. The function returns a pointer to an integer, but the returned pointer points to the statically allocated int called even. This local variable goes out of scope at the end of the function. Therefore, when the caller tries to access it, it will already be gone. Instead, the function should just return a pointer to the even integer in the vector itself. When we reach the first even number, the iterator points to the correct location. In order to return a pointer to the caller's vector, the parameter must be passed by reference. A const reference protects the caller's data. Here's the corrected code: int* first_even(const vector &vec) { vector::iterator itr(vec.begin()); while ((itr!=vec.end()) && (*itr%2!=0)) { cout<<*itr<<" "; itr++; } if (itr==vec.end()) return NULL; else return *itr; } 2. The flaw here is that we are accessing elements of answer_vec using the [ ] operator, but answer_vec is actually empty - we have never put any elements in it. The program will crash at this point. Fix it by using push_back to append the new numbers onto answer_vec, instead of using []. This allocates space for the new element and copies the new value. vector max_vec(const vector & vec1,const vector & vec2) { int min_length, i; if(vec1.size() answer_vec; for(i=0;ivec2[i]) answer_vec.push_back(vec1[i]); else answer_vec.push_back(vec2[i]); } return answer_vec; } 3. There are two problems. The first is a memory leak, which occurs When "Marcus Hunter" is created. At this point, we store the address of Marcus Hunter in ep1, and the address of Maribeth Higgens is lost. Maribeth and the memory associated with her is lost, forever! To fix this problem, delete the memory before Marcus Hunter is allocated. The second problem is even more insidious. The comment indicates that we have made a copy of Marcus Hinter. In fact, we did a shallow copy, which means that both ep1 and ep2 point to the same memory location. When we change the last name to "Hinter", the last name for both ep1 and ep2 changes. The problem becomes more insidious after the call to delete ep1. At this point, the memory is released, and ep2 is now a dangling pointer. When we subsequently call ep2->display(), the program will likely crash. To fix the problem, instead of a shallow copy with ep2 = ep1, instead we allocate a new employee object and initialize it with a copy of ep1. Lastly, let's de-allocate the employee Marcus Hinter before exiting the program. int main() { Employee *ep1, *ep2; // create a new employee, initialize its data, display it ep1 = new Employee("Maribeth", "Higgens", 10.5); ep1->display(); delete ep1; // This fixes the first problem - a memory leak // create another new employee, initialize and display ep1 = new Employee("Marcus", "Hunter", 9.5); ep1->display(); // make a copy the existing employee and change the last name // ep2 = ep1; This line of code does a SHALLOW copy, we need a deep copy ep2 = new Employee(ep1); // This is a deep copy - copy constructor called ep2->lastname("Hinter"); // get rid of the original employee delete ep1; // display the second employee ep2->display(); delete ep2; // get rid of Marcus Hinter return 0; } 4. // copy constructor Employee::Employee(const Employee ©) // copy constructor { myFirstName = new char[strlen(copy.myFirstName)+1]; strcpy(myFirstName, copy.myFirstName); myLastName = new char[strlen(copy.myLastName)+1]; strcpy(myLastName, copy.myLastName); myWage = copy.myWage; } // assignment operator const Employee &Employee::operator=(const Employee &rhs) { if (this != &rhs) { if myFirstName delete myFirstName; if myLastName delete myFirstName; myFirstName = new char[strlen(rhs.myFirstName)+1]; strcpy(myFirstName, copy.myFirstName); myLastName = new char[strlen(rhs.myLastName)+1]; strcpy(myLastName, rhs.myLastName); myWage = rhs.myWage; } return *this; }