← Debugging with gdb/lldb & Memory-Checking Tools

Fix: Stale State Between Queries

hard

This program answers several independent “what’s the max of these values” queries in a row. Run the sample by hand first: query 1’s values are 10 20 5 (max 20, correct), but query 2’s values are 1 2 3 – their actual max is 3, yet the buggy program reports 20 again.

There’s no compile error here, and nothing undefined – this is the class of bug the lesson closes on: state that silently outlives the call that set it. static gives a local variable a lifetime spanning the entire program instead of just one call, which is exactly what you want for, say, a counter that persists across calls on purpose – and exactly wrong here, where each call is supposed to start fresh. Find where best is declared, and fix its lifetime, not the comparison logic (which is already correct).

Your solution

"Run" uses the sample stdin ("2\n3\n10 20 5\n3\n1 2 3"). "Submit" checks your code against all 3 test cases.

Test results

Submit your solution to run it against all test cases.

Hints

    Reference solution