C/C++ How to debug a c or c++ program?

  • Thread starter Thread starter kant
  • Start date Start date
  • Tags Tags
    C++ Program
AI Thread Summary
To debug C and Java programs effectively, using print statements is a common approach. In C, incorporating "printf" followed by "fflush(stdout)" ensures that output is displayed immediately, which is crucial for identifying where a program may crash. This prevents confusion about the location of errors, as buffered output may not appear if the program terminates unexpectedly. In Java, debugging is facilitated by "System.out.println" for output, and developers can utilize stack traces to trace exceptions. The use of try-catch blocks is also recommended for better error handling. For more advanced debugging, utilizing a compiler with the "-g" option in GCC and tools like DDD can help visualize data structures, making it easier to track variables and understand program flow. DDD is particularly useful for linked lists and can handle various data structures, including trees.
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: 480
Back
Top