How to debug a c or c++ program?

  • C/++/#
  • Thread starter kant
  • Start date
  • #1
360
0
I learn to code in the c language. i am learning the workings of C++, and Java.

I already have a c/c++ compiler in my computer. My problem is:
How do i "check for errors"/debug a c or java program? Are there any good tutorial online?
 

Answers and Replies

  • #2
1,356
2
what compiler are you using? if they're small codes "printf"(for c) is your friend
 
  • #3
If the code is small, use printf as he said, but follow these printf calls with the line:

fflush(stdout);

Otherwise if your program crashes in runtime it might not print everything that it is supposed to (output is buffered, and fflush clears out the buffer). You have no idea how confusing it can be to think a program is failing at point A when it is actually failing a little later at point B.
 
  • #4
-Job-
Science Advisor
1,146
1
Java programs are easier to debug than C programs. You can use System.out.println to print debugging messages. If your program is crashing then you can follow the stack trace to see where the exception originated. You can also use try{}catch(Exception e){...} throughout your code. One reason why i love Java.
 
  • #5
1,356
2
youar.Master: what does the fflush(stdout) display? i've never seen that before(i'm a novice or inter. programmer).
 
  • #6
neurocomp2003 said:
youar.Master: what does the fflush(stdout) display? i've never seen that before(i'm a novice or inter. programmer).
fflush(stdout) doesn't display anything as Your.Master said, it locks the program and forces data sitting in the stdout buffer to be written which ensures that you will see output from the printf. For example if you have a printf line followed immediately by a line that causes a segmentation violation, the program may receive a signal to kill it before the previous lines printf has been flushed, this may lead you to think the program is crashing somewhere before that printf. For more information 'man fflush'.
 
  • #7
dduardo
Staff Emeritus
1,890
3
This is specific to unix/linux, but when I debug code I use a combination of printf statements as well as compiling the program with a -g option in gcc and then using DDD to trace variables and such.

http://www.gnu.org/software/ddd/

ddd is really nice for tracking linked lists because it will actually show you graphically how the nodes are linked and what information they contain.

all.png
 
  • #8
-Job-
Science Advisor
1,146
1
That's very nice, does it do other data structures, like trees?
 
  • #9
dduardo
Staff Emeritus
1,890
3
Yeah, it can handle pretty much any data structure you feed it.

Here is an example of trees (I've had to scale it down a lot because of my 1600x1200 resolution:
 

Attachments

Related Threads on How to debug a c or c++ program?

  • Last Post
2
Replies
31
Views
2K
  • Last Post
Replies
3
Views
876
Replies
2
Views
2K
Replies
8
Views
936
Replies
8
Views
4K
Replies
34
Views
498
  • Last Post
Replies
1
Views
602
Replies
2
Views
2K
  • Last Post
Replies
3
Views
3K
  • Last Post
2
Replies
32
Views
513
Top