Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# How to debug a program?

Tags:
  1. May 17, 2017 #1
    I want to know what kind of programs can be debugged and how to do this?
     
  2. jcsd
  3. May 17, 2017 #2

    DaveC426913

    User Avatar
    Gold Member

    That is an extremely broad question.

    The short answers, in order, are:
    - all of them and
    - by myriad methods, depending on the language and tools available.

    Perhaps you could refine your question a little.

    Do you want to learn computer programming?
    Do you have some source code that you want to fix?
    Do you have a particular language in mind?
     
  4. May 17, 2017 #3

    YES, I want to learn computer programming.
     
  5. May 17, 2017 #4

    DaveC426913

    User Avatar
    Gold Member

    What kind of software do you want to write? This will influence what language you choose, which will in turn influence what books you might buy to read.
     
  6. May 17, 2017 #5
    I want to learn C++.
     
  7. May 17, 2017 #6

    berkeman

    User Avatar

    Staff: Mentor

    Welcome to the PF. :smile:

    As Dave is gently pointing out, when you post here at the PF, it is best to give as much background and information as you can about your question.

    For starters, read through this link at Wikipedia to see if it helps get you started with your question...

    https://en.wikipedia.org/wiki/Debugging
     
  8. May 17, 2017 #7

    DaveC426913

    User Avatar
    Gold Member

    There are plenty of online tutorials. You would do well to search through them for an Intro to programming.
    If you have not programmed before, c++ might be a bit of a learning curve. You might start with BASIC.

    I have found some courses from Udemy were worth the paltry price I paid for them.
     
  9. May 17, 2017 #8

    jedishrfu

    Staff: Mentor

    You could also start with Java, Python or JavaScript. All three are available in the Processing environment as installable modes each with a collection of examples and libraries to do some amazing stuff.

    Check out the PF Insights page for The Joy of Processing to learn more.

    Learning Java or Python wouldn't be a waste of time since they are somewhat derived from C just like C++. The basic difference between Java and C++ is in class inheritance where C++ allows for multiple parents whereas Java limits it to one but multiple inheritance is seldom used.
     
  10. May 17, 2017 #9

    phinds

    User Avatar
    Gold Member
    2016 Award

    Well, yeah, if you're using the OOP characteristics of C++ but I've always thought that the BASIC difference between C++ and JAVA is that in JAVA you have no choice about whether you are using OOP whereas in C++ that is not enforced. Code can be written in "C++" that really is just C code. You can't do that in JAVA.
     
  11. May 17, 2017 #10

    jedishrfu

    Staff: Mentor

    True, but how many differences do I need to say in order to cover all of them?

    To me the biggest difference was the issue of multiple inheritance that many programmers felt was great in theory but bad in practice. We used to practice single inheritance with mixins in our designs. Mixins were classes that added some specific feature to child clas but wouldn't confuse the inheritance tree.
     
  12. May 17, 2017 #11

    jedishrfu

    Staff: Mentor

    With respect to debugging, any program can be debugged sometimes via a tool that allows you to step through program execution, other times only via print statements.

    In some cases, you might be really constrained as in an embedded system with no display or keyboard and would need to develop something special to help you debug like logging results to a file and reading it by text editor or something.

    I had to do that once with a small microcomputer with toggle switches and led imdicators. It was very time consuming and somewhat painful as you had no idea why the program failed and you had to scrutinize your code for possible causes.
     
  13. May 18, 2017 #12

    ChrisVer

    User Avatar
    Gold Member

    It's better to ask the poster:
    Do you know what a bug is?
    I guess if you did you wouldn't ask when you need to debug...

    when your program produces unexpected results or does not compile, then (99.99% of the times) there's a bug in your code: something is wrong in your code and you got to fix it. Fixing those is called debugging.
    One very simple example is the following, which is a function that returns you the square of a number:
    Code (C):

    #include<iostream>

    int square( double x){
       //returns square of number x
       return x*x;
    }
    int main(){
       double sqx = square( 2.0 );
       std::cout<< sqx << std::endl;
       return 0;
    }
     
    your code will compile, you can run several tests with it, trying the square of 2,3,4,5 and see that the code is doing a perfect job in printing the squares of those numbers... all good until someone cranky chose to check the square of 0.4 believing he would get 0.16 back... you can try it and see that your code will fail producing the expected result... the problem is in the code and in particular the type definition of the function square, which casts the result to that of type int , so the resulted double 0.16 will try to be translated (casted) into an integer, which is 0 (your output)...
    Going and replacing the int square with double square is 1 extremely simple example of debugging your code.

    There are other cases which can scale in difficulty depending on the readability of the code up to its complexity... Making your code modular can help in some extent the debugging process, because it can be clear (most of the times) where you have to go and check for bugs...

    That's some basic input from me.
     
  14. May 18, 2017 #13
    This topic is interesting since I feel like I've just been winging it when it comes to debugging. Probably why every complex program I've worked has a billion bug tickets. Things I do...

    - Reproduction: Ideally you can reproduce the bug. Can be tricky if the bug is intermittent (like say a race condition). If you can't trigger the bug at all, you might be able to deduce what went wrong if you had logs (see below) when the bug occurred and read the corresponding code.

    - Read code: If the program is simple, then you might be able to deduce where in the code the error occurs just from the bug description, then you can go straight to reading that segment of the code and fix it if the issue is identified.

    - Logging: Having your program write information about what it is doing to a text file. It can be useful when say a customer reports a problem, as it allows you to see to some extent what was going on under the hood. Or it can be useless if you don't log the relevant information, but it hopefully informs you on what additional logging to add so that you have better luck the next time the bug occurs.

    - Debugger: Only relevant if you can reproduce the bug. With a debugger program, for example, you can execute your code line-by-line and examine the contents of each variable, how they change, and hopefully you'll then recognize the step(s) that need to be adjusted to avoid the bug.

    - Testing: After the fix is completed, make sure your suite of unit and regression tests still past. You don't want to fix one bug only to introduce 10 more with the fix. Obviously automated testing and testing from QA engineers doesn't prevent that because it is not practical to test all possible inputs (except for a trivial program), but it does provide some confidence (but not too much when your bug tracker is overflowing).
     
  15. May 23, 2017 #14
    All programs can be debugged. The reason that you can't simply throw any program on your computer into a debugger is because debugging requires extra stuff be put into the code so that the debugger can give you back useful information. This gets stripped out during release. This is why when you first create a project in almost any IDE, you have a Debug and Release build option by default.
     
  16. May 23, 2017 #15

    fluidistic

    User Avatar
    Gold Member

    In my case (beginner and self taught with lots of bad/wrong habits), I debug using :
    0) Lots of print statements. I have to make sure what I Think the code is doing is really doing it.
    1) Test programs. Sometimes when I'm unable to figure out where the bug(s) is, I try to create a minimal working example. I basically copy and paste (or try to recreate as closely as possible some part(s) of the original code) the original code into a new file where I can manipulate the code more easily than in the original code.
     
  17. May 23, 2017 #16

    DaveC426913

    User Avatar
    Gold Member

    I still do this. I'm old school. It actually cost me a potential job. Employer was not impressed to see me debugging using console.log (JavaScipt equivalent of Print)

    The modern way is to use breakpoints and step through the code, to watch what is happening as it happens.
     
  18. May 24, 2017 #17

    Mark44

    Staff: Mentor

    Sometimes the equivalent of print is the only debugging tool available. About four years ago I was trying to write some code samples for SharePoint (a MSFT product), and my code was running against a SharePoint server running on a cloud OS, that would send back a stream of text to populate a web page. For quite a while I wasn't getting anything back, and couldn't figure out any way to set a breakpoint on the server. (There probably is a way, but a fair amount of study on my part didn't unearth it.) My solution was to insert output statements throughout my code, and determine what part of my code was working and where it stopped working by the output I got. Once I figured out where my output statements weren't being hit, I could focus my efforts there and figure out why. After a number of iterations using this process, I was able to get my code working correctly.
     
  19. May 24, 2017 #18
    If it was solely because of that, the guy was an idiot anyway. I'm a professional programmer and I've worked with dozens of other great experts. Everybody does cout, prints, var_dump...

    For me, doing a bunch of variable dumps in real time allow me to intuit the problem far faster than setting breakpoints and stepping could ever.
     
  20. May 24, 2017 #19

    DrClaude

    User Avatar

    Staff: Mentor

    Real programmers don't use debuggers :wink:

    (For the youngings who don't understand the joke, see http://web.mit.edu/humor/Computers/real.programmers).
     
  21. May 24, 2017 #20

    ChrisVer

    User Avatar
    Gold Member

    What would that mean in practice?

    I agree with the others : you were lucky to avoid such an employer...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to debug a program?
  1. How To Program? (Replies: 7)

  2. C++ and c debugging? (Replies: 3)

Loading...