Answer these short-answer questions. You can either submit a text file with your answers (answers.txt) or enter your answers into the text-box provided by Canvas.
1. What does the following code print?
#includevoid f(int z, int *pf){ *pf = 1; z = 2; } int main(){ int x = 0; int y = 0; int *p = &x; *p = 5; f(y,&y); std::cout << "x: " << x << "\n"; std::cout << "y: " << y << "\n"; return 0; }
Refer to the following code for the next few questions.
int main(){ int x; int y; int z = 0; int *p1 = new int; int *p2; int *p3 = &x; int **ptp = &p2; *ptp = new int; **ptp = 1; *p3 = 3; // What is uninitialized (undefined, garbage) here? return 0; }
2. Which values in the code are undefined (garbage) at the commented point? Hint: Please include uninitialized heap values (i.e. memory that has been allocated on the heap but not initialized).
3. What is the value of x?
4. What is the value of (*p2)? (The value of the memory pointed to by the pointer p2)
5. Which of the following are correct statements about malloc/free and new/delete?
In this next section, you will debug and fix a program that has multiple memory safety errors in its use of pointers. You can debug the program using a combination of 1) your eyes, 2) gdb, and 3) valgrind. When you are finished, the program should run through valgrind with no errors.
Download the buggy code sample to your eniac drive using the following commands:
wget http://www.cis.upenn.edu/~cis190/spring2015/hw/hw3files/buggy.cpp .
You can also pull the file from the github repository, under hw3.
Compile the code using the following command:
g++ -o buggy buggy.cpp -g -std=c++11
To debug the program, you can try a few different commands:
gdb ./buggy valgrind ./buggy
Hint: You should look for all possible types of pointer errors - buffer overflows, use-after-free, memory leaks, and uninitialized memory.
Hint: Run valgrind with the --leak-check=full option to see line number output for memory leaks.
When you are finished, please submit the fixed version of the buggy code to Canvas. If you are also submitting your answers to the first part of the homework in a text file, submit a zip file with both files.
Pointer arithmetic allows us to adjust the address pointed to by a pointer. For example, if we have an int pointer that points to 0x10000000 and increment the pointer once, it will then point to address 0x10000004. The following code example demonstrates pointer arithmetic further.
#includeint main(){ int *p1 = new int[3]; int *p2 = p1; assert(p1[0] == *p2); p2++; assert(p1[1] == *p2); assert(p1[1] == p2[0]); assert(p1[2] == p2[1]); p2++; assert(p1[2] == *p2); return 0; }
If you run this code, none of the asserts will fail.
Download the challenge code sample (challenge.cpp) using either wget or from the github repository. Find the comment in the code and print the value requested by the comment. The only code you should need to add is code that performs pointer arithmetic (or indexing) and prints the value using std::cout. You should not add any new function parameters or variables.
Submit the file challenge.cpp with the added statement that prints the correct value for extra credit.