How to debug a c or c++ program?

  • Context: C/C++ 
  • Thread starter Thread starter kant
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary

Discussion Overview

The discussion centers on methods for debugging C and C++ programs, including techniques, tools, and personal experiences. Participants share their insights on debugging practices, particularly in relation to small code snippets and the use of specific functions and tools.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant inquires about general debugging techniques for C and C++ programs and seeks online tutorials.
  • Another suggests using "printf" for small codes as a debugging aid.
  • A participant emphasizes the importance of using "fflush(stdout)" to ensure that output is printed before a potential crash occurs, explaining that output may be buffered.
  • It is noted that Java programs may be easier to debug than C programs, with suggestions to use "System.out.println" and stack traces for error tracking.
  • A participant asks for clarification on what "fflush(stdout)" does, indicating a novice level of understanding.
  • Another participant explains that "fflush(stdout)" does not display anything but forces the output buffer to flush, which can prevent confusion during debugging.
  • A specific debugging approach is mentioned that involves using the "-g" option in gcc and the DDD tool for visualizing data structures.
  • Participants discuss the capabilities of DDD, including its ability to handle various data structures like linked lists and trees.

Areas of Agreement / Disagreement

Participants generally agree on the usefulness of "printf" and "fflush(stdout)" for debugging, but there are differing opinions on the ease of debugging between C and Java. The discussion includes multiple approaches and tools without a consensus on a single best method.

Contextual Notes

Some participants express varying levels of familiarity with debugging techniques, indicating a range of experience from novice to intermediate. There is also a mention of platform-specific tools, such as DDD for Unix/Linux.

Who May Find This Useful

Individuals learning to program in C or C++, as well as those interested in debugging techniques and tools for these languages.

kant
Messages
388
Reaction score
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?
 
Technology news on Phys.org
what compiler are you using? if they're small codes "printf"(for c) is your friend
 
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.
 
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.
 
youar.Master: what does the fflush(stdout) display? I've never seen that before(i'm a novice or inter. programmer).
 
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'.
 
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
 
That's very nice, does it do other data structures, like trees?
 
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

  • tree.png
    tree.png
    13.4 KB · Views: 505

Similar threads

Replies
86
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
69
Views
11K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K