You are not logged in. Please login at www.codechef.com to post your questions!

×

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.

asked 05 Dec '12, 19:28

rajat_121292's gravatar image

2★rajat_121292
1689
accept rate: 0%

edited 09 Jan '13, 12:39

admin's gravatar image

0★admin ♦♦
18.5k348495529


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 60". 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. :)

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.

link

answered 09 Dec '12, 12:03

vinayak%20garg's gravatar image

4★vinayak garg
3.7k113249
accept rate: 18%

Good explanation .

(09 Dec '12, 17:44) ani942★

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

(09 Dec '12, 19:08) cherrypowers0★

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. :) if you're courageous, i suggest you to try the same technique. :)

link

answered 09 Jan '13, 00:51

cyberax's gravatar image

2★cyberax ♦
3.4k21955
accept rate: 20%

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. :)

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 ;)

Best regards, Bruno

link

answered 09 Jan '13, 03:16

kuruma's gravatar image

2★kuruma
17.5k72143209
accept rate: 8%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×211
×84

question asked: 05 Dec '12, 19:28

question was seen: 6,219 times

last updated: 09 Jan '13, 12:39