 | CoSc2030: Computer Science II Fall Semester 2007 Instructor: Dr. Thomas Bailey |
Lab 03: Object Lifetime
This lab asks you to take a closer look at the object architecture and how C++ uses this architecture during program execution.
To get started get the following framework files: Simple.h, Simple.cpp, and SimpleDriver.cpp. Your TA will provide a small background overview of the structure of this code, as well as some perspective on the questions below.
Lab Assignment
To answer the following questions you will need to modify SimpleDriver.cpp. Your answers should be 3 to 5 sentences long, and should not only state what happens, but attempt to explain why it happens.
- Write a call that creates a new instance of class Simple on the heap (but do not destroy the object). Explain the output, and explain why does this code create a memory leak.
- Correct the problem in item 1. How and why does your fix work?
- Write a for loop that iterates 5 times and in the body of the loop declare an object of type Simple. Explain the output, addressing the issue of object scope. Does your routine leak memory? Why or why not?
- Repeat item 3 above, but in the body of the loop create the object on the heap. Again explain the output, addressing the issue of variable/object scope. Does this implementation leak memory? If no, explain why. If yes, how can you fix it?
- Add a function with the following signature void do_nothing(Simple object) and leave the body of the function empty. Invoke this function from the main routine, and explain the output.
- Repeat item 5 above, but this time use the following function declaration void do_nothing_again(Simple &object) (and leave the function body empty). Provide an explanation for the output and compare and contrast the results for items 5 and 6.
- Introduce the following function with an empty body void do_nothing_yet_again(Simple *object). As before, analyze the result of calling this routine from main. How is it different from the output obtained in items 5 and 6? Summarize your findings regarding C++ parameter passing mechanisms.
- To continue our examination of the interaction between functions and objects, create, run, and analyze the following function:
Simple mystery(const Simple &object) { const Simple ©(object); return copy;} - Using information from items 5-8, explain the difference between the following two functions, and discuss the specific problems of each implementation:
Simple &factoryA() { Simple object; return object;} | Simple *factoryB() { Simple *object = new Simple; return object;} |
- And just to confuse things a little, implement the following piece of code and explain what is going on:
Simple object;Simple *ptr(&object);Simple &ref(object);cout << "object = " << object << endl << "&object = " << &object << endl << "ptr = " << ptr << endl << "ref = " << ref << endl;
Turn In:
Upload the modified SimpleDriver.cpp. Please rename the file Lab3SimpleDriver.cpp and put your name in comments at the top of your file. Also please upload your answers to the ten questions above. You may write you answers as comments in the code or upload a separate text document.
<< CoSc2030 Home
Copyright © 2005 Thomas Bailey