How to debug a code after many attempts

How to debug a code if , i have debugged it many times, and what should be the next step i should follow.

You have not mentioned the reason for debugging. But I can think of two (or three)-

Runtime error (like segfault)

Debugger like gdb can be use to step through the code, line by line.

  1. You can set breakpoints at specific line numbers, or at some function - When execution reaches breakpoint, it breaks. Now you can examine local variables, call stack etc. From there you can “step over”, “step into”, or “continue”.
  2. In cases of errors like SIGSEGV or SIGABRT etc - The execution breaks by itself. And you can examine line number, local variables and call stack.

Add “-g” flag while compiling to
produce debugging symbols. Then start
gdb with output file name. “-tui” is an
optional flag.

Set breakpoint on line 60 by “b
. Start execution by "run"

Press “n” to step to next line.
"s" to step into, and “c” to continue. “p myvar” prints
variable "myvar"

And “q” to quit. :slight_smile:

Wrong Answer (due to some bug in code)

You are getting wrong answer (on your system). Again you can use gdb to step line by line to find bug. But this is tedious. You should examine your code carefully. Look for uninitialized variables.

Wrong Answer (for no reason)

Well there has to be some reason. Debugging might not help. You should reconsider your logic and reread the statement. But it might be because of a bug also.


Good explanation .

Oh, I see! haven’t thin k of it. Thanks anyway!

you’ll probably think i’m a psycho (huhu), but when i think i’ve tried everything and still can’t find the bug in my code, i keep it in a file, and try to write it again from scratch. then, i compare both. and often enough, i succeed in seeing what was wrong in the first code, and i feel stupid for not seeing it before, and happy i found it after. :slight_smile: if you’re courageous, i suggest you to try the same technique. :slight_smile:

1 Like

What cyberax said, is actually one of the best ways, if not the best way, of spotting errors during a live programming contest, in my opinion. :slight_smile:

And while knowing how to use a debugger like gdb can be very useful when you are working on a large project or have a lot of scaffolding added in your code, the main reason people get WA on an algorithms contest is exactly because of the underlying idea they are using…

Writing code correctly comes with practice only but it gets easier over time… Then you are more aware about initializing variables or checking for array bounds on your own (also, adding small comments on your code while writing it can help you greatly), without a need of a debugger.

With all these details put aside (with gdb or without it), the issue you’re facing can only be related to the algorithm itself and the best way to fix this, is to switch problems for a bit and also re-write everything from scratch, as on the 2nd time you can notice some new issues that went unnoticed the 1st time :wink:

Best regards,