Jeff had been onerous around the office the whole day. The countless scrum meetings and hours spent fiddling with his development environment to make it work with the latest release has left him tired and irritable. He really needed this moment of solitude.
He opened his laptop with anticipation. His latest personal project had been misbehaving, it needed some special attention. The project was some C code and involved an elegant recursive function. He was excited. C was unforgiving, dominating, and efficient. He wanted to know C on an intimate level. He just had to get inside this misbehaving program.
Jeff fired up GDB. He set a few breakpoints and watched a few variables. It was time to start.
He first tried the base case and all seemed well. The variables were what he expected and the return value was right.
Okay. He thought to himself. Let’s try it with one level of recursion.
Jeff ran the program again with one level of recursion.
Woah, that’s a big stack.
The function had somehow called itself an indeterminate amount of times. It eventually did reach the base case, as a stack overflow hadn’t occurred, but he was baffled at how it had gotten so deep.
He stepped the program, checking the variables as they changed. The stack popped. He got tense with anticipation, waiting to reach the end. The stack popped, over and over. He didn’t care about the variables and sped up. The stack popped faster and faster until finally, he found the bug.
Spent and tired, Jeff crawled into bed. He’ll fix it the next evening, he has time. Besides, he knew what the problem was and already knew the fix.
Off by one.