060_Problem_Set_1_Solutions

060_Problem_Set_1_Solutions - CS106L Fall 2009 Handout #06...

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

View Full Document Right Arrow Icon
CS106L Fall 2009 Handout #06 November 17, 2009 Problem Set 1 Solutions _________________________________________________________________________________________________________ Problem 0 a. The reason that the code doesn't work correctly is that "" + 5 is interpreted as “the pointer five steps past the beginning of the empty string” rather than “the string formed by concatenating the empty string and the number five.” Taking five steps past the beginning of the empty string walks off the end of string and into random memory. The C++ standard says that this falls into the realm of undefined behavior, so the actual string you get back might depend on your operat- ing system, what compiler you're using, or the alignment of the planets. Of course, you might end up with a pointer to memory that you don't own, so printing this string might completely crash the program. b. The problem with the function is that the while line char* buffer = new char[ strlen(source + 1) ]; looks like it allocates enough storage space for the C string, including the null terminator, it in- stead does something else. The + 1 in the above statement means “take one step past the begin- ning of source ” rather than “reserve one more byte than the length of the string.” You can see this better by comparing it against a correct implementation: char* buffer = new char[ strlen(source) + 1 ]; The string source + 1 is the same string as source , but with the first character removed. As- suming that source is n characters long, this means that the faulty implementation only allocates space for n – 1 characters. It is therefore underallocating by two bytes – one byte for the missing character of source and one byte for the null terminator. Copying the contents of source into this buffer therefore writes into invalid memory and will almost certainly crash the program. - 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
Problem 1 I was pleasantly surprised by how many of you were able to come with the solution to this problem. The actual code you need to write is not particularly long, but it's fairly dense and you need to have a solid un- derstanding of templates to appreciate how it works. Here is one possible implementation: template <typename ForwardItr1, typename ForwardItr2, typename OutputItr> OutputItr cartesian_product(ForwardItr1 start1, ForwardItr1 stop1, ForwardItr2 start2, ForwardItr2 stop2, OutputItr where) { for(ForwardItr1 one = start1; one != stop1; ++one) { for(ForwardItr2 two = start2; two != stop2; ++two) { *where = make_pair(*one, *two);
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.

Page1 / 5

060_Problem_Set_1_Solutions - CS106L Fall 2009 Handout #06...

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