Description
Each student begins with four late days that may be used throughout the quarter. You may submit this assignment 24 hours late by using one late day or 48 hours late using two late days. No submissions will be accepted more than 48 hours after the due date without prior approval by the head TA. See the syllabus for more information about our late policies.
All due dates and submission times are expressed in Pacifc time.
2023/3/25 17:27 CS106B Welcome to C++!
This call makes yet another call to sumOfDigitsOf(0) for the same reason:
And that call makes yet another call to sumOfDigitsOf(0):
As you can see, this recursion is of to the races. It’s like an infnite loop, but with function calls. This code will trigger a stack overfow because at some point it will exhaust the memory in the call stack.
Another place you’ll see stack overfows is when you have a recursive function that, for some reason, misses or skips over its base case. For example, let’s suppose you want to write a function isEven that takes as input a number n and returns whether it’s an even number. You note that 0 is even (trust us, it is; take CS103 for details!) and, more generally, a number n is even precisely if n – 2 is even. For example, 2 is even because 2 – 2 = 0 is even, 4 is even because 4 – 2 = 2 is even, and 6 is even because 6 – 2 = 4 is even, etc. Based on this (correct) insight, you decide to write this (incorrect) recursive function:
2023/3/25 17:27 CS106B Welcome to C++!
Due Friday, January 20 at 1:00
Assignment Logistics
Part One: Debugger Warmups
Part Two: Fire
Part Three: Only Connect
(Optional) Part Four: Extensions!
Submission Instructions
This call then calls isEven(-3):
And we’re of to the Stack Overfow Races – n will keep getting more negative until we’re out of space.
Step One: See a Stack Overfow
There are two diferent ways that you can run a C++ program through Qt Creator. The frst is to run the program normally, which you can do by clicking the large green triangle “run” button. The second is to run the program with the debugger engaged, which you saw how to do in Assignment 0. The behavior of a program that triggers a stack overfow is diferent based on whether it’s running with the debugger on or of.
To see this, run the provided starter fles without the debugger engaged (that is, with the green triangle run button). Click the “Stack Overfows” option. You’ll see a message and a button at the bottom of the window that will trigger a stack overfow. Click that button and watch what happens. This is what it looks like when a C++ program crashes. What you see will depend on what operating system you’re using. Write down a description of what you saw happen as Q2 in the fle comments of DebuggerWarmups. Now, if you see similar behavior in the future, you’ll be able to say “oh yeah, that probably means my program crashed.”
Next, run the program again, but this time with the debugger turned on. Follow the same steps as above to trigger the stack overfow. This time, you should see the debugger pop up and point at a line in the program where the stack overfow occurred. You’ll also see the call stack, which should be flled with lots and lots of calls to the same function. Now that you’ve got the debugger engaged, you can investigate which function triggered the stack overfow and, ideally, to work out what went wrong. You don’t need to write anything down just yet. For now, hit the red “stop” button to close the program.
Going forward, as you’re writing your frst recursive functions, keep what you just saw in mind. If you see something that looks like your program crashed with a stack overfow, turn on the debugger and run it again. You’ll then be taken to the exact spot where the stack
2023/3/25 17:27 CS106B Welcome to C++!
