How to debug a program?

  • #26
jtbell
Mentor
15,614
3,638
(http://web.mit.edu/humor/Computers/real.programmers)

No, the Real Programmer wants a `you asked for it, you got it' text editor -- complicated, cryptic, powerful, unforgiving, dangerous. TECO, to be precise.
I wrote my physics PhD dissertation with TECO, using RUNOFF commands for formatting the margins, etc.

(RUNOFF was a text-markup and formatting language, in a similar spirit as HTML.)
 
  • #27
489
189
The modern way is to use breakpoints and step through the code, to watch what is happening as it happens.
Unfortunately this doesn't always work. Today I was stumped why a "release version" crashed (vague exception too :().
I narrowed it down to one of two methods and set a breakpoint. It didn't work because I was communicating with some hardware that required an acknowledgement within a second or would retransmit. stepping through the code took longer :-(
Eventually I noticed my mistake when glancing at my Task list (TODO comments are great by the way). I hadn't implemented the final part of a small helper class which resulted in a NullReferenceException (which somehow got eaten by an unknown exception).

Logging got me through that problem. It's crazy how often I get lost in these great tools and fall back to extreme amounts of logging (Another time my (combined) logfiles for a single testrun of 30 seconds were several MB, which in the eventual code would fill up the HDD very fast as the code keeps getting started)
 
  • #28
1,518
618
Yes, breakpoints with parallel programming is less useful than it is with procedural. That's where I tend to start throwing printf() at the problem. Of course, often adding prints changing the timing of everything and hides the big you're trying to find. Thats when you pull out the big guns: valgrind.
 
  • #29
ChrisVer
Gold Member
3,347
448
valgrind
is valgrind good for debugging? I thought it was for memory control... (so maybe for hunting seg faults?)
 
  • #30
1,518
618
is valgrind good for debugging? I thought it was for memory control... (so maybe for hunting seg faults?)
It's not a debugger in the same way that gdb is, but it's invaluable for hunting multi threading and weird memory bugs.
 
  • #32
832
30
I've learnt to program in fortran, I don't know any language, but now when I see a code in C I can understand much of it. Fortran is really intuitive, there are a very few things you have to learn, and then you go. Today is very easy to learn just by looking for tutorials on the internet. The best way to learn is just by programming your self. It can be an arduous work, depending on what you want to do. But this forum is also a great place for interacting with people that can guide you in your learning process. As many before have told you, it is important that you know what specifically you want to write a program for.
 

Related Threads on How to debug a program?

Replies
8
Views
17K
Replies
8
Views
910
  • Last Post
Replies
1
Views
572
  • Last Post
Replies
3
Views
860
Replies
8
Views
4K
  • Last Post
Replies
3
Views
3K
  • Last Post
Replies
5
Views
3K
Replies
2
Views
2K
Replies
4
Views
2K
  • Last Post
Replies
13
Views
4K
Top