040_Problem_Set_1

040_Problem_Set_1 - CS106L Fall 2009 Handout #04 November...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106L Fall 2009 Handout #04 November 5, 2009 Problem Set 1 _________________________________________________________________________________________________________ Due November 12, 2009, 11:59PM In this problem set, you'll get a chance to play around with the concepts we've covered over the past few lectures. Some of these problems exercise specific aspects of the material, while others are more open- ended and should help give you a better feel for C++ as a whole. This problem set has five problems, of which you need to complete four to receive credit. As always, feel free to email me if you have any questions. Question 0 (C Strings) You might want to read Chapter 12 of the course reader before attempting this problem. a. In lecture, we saw that the following program prints nonsensical output: #include <iostream> #include <string> using namespace std; int main() { string myString = "" + 5; // <-- Error here! cout << myString << endl; } Unfortunately, although the first line of main looks like it concatenates the number five onto the empty string, the code does something entirely different. What does the first line of main actu- ally do? Why does this cause the program to print garbage characters? b. Recall from our discussion of C strings that creating a deep copy of a C string is a two-step pro- cess. First, we allocate storage space for the copy of the C string, including space for the null ter- minator. Second, we copy the string to the new buffer using strcpy . A friend calls you up asking for help on a C++ programming project. His program contains a bug that periodically causes it to crash. You look over his code and notice the following function, which is supposed to duplicate a C string: char* StringDuplicate(const char* source) { char* buffer = new char[strlen(source + 1)]; // <-- Error here! strcpy(buffer, source); return buffer; } You notice that this function contains a bug, which is highlighted in bold. Using your newfound knowledge of pointer arithmetic, explain why this code does not work correctly. (Hint: Compare this code to the code we wrote in lecture and see if you notice any differences. The error is very subtle.) - 1 -
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Question 1 (STL Algorithm Implementation) You might want to read Chapter 14 and attempt some of the practice problems before starting this prob- lem. The
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/12/2010 for the course CS 106l at Stanford.

Page1 / 5

040_Problem_Set_1 - CS106L Fall 2009 Handout #04 November...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online