Thread: Debugging Technique

  1. #1
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Debugging Technique

    I'm curious about how the experts here feel about using printf()/cerr for debugging purposes. Assume a new user, would you recommend printing vs. learning the debugger at a newbie's stage of development.

    And what do you think of this writeup. Is the author correct -- especially for the new student?

    Or how about this one (specifically at printf debugging heading)?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    They don't seem unreasonable.

    Printing things is easy to explain and produces and obvious benefit without having to learn yet another tool. It's also fine for the single file assignments with only a couple of functions and a handful of variables.

    Debuggers come in when programs get larger, and repeated attempts at inserting printf() in the wrong place causes people to seek out a better way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Thanks Salem, that was my contention when someone tried to prove me "wrong" about suggesting printf(). Thought I'd get another opinion from respected programmers...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm curious about how the experts here feel about using printf
    >()/cerr for debugging purposes.
    As with any debugging technique, debug prints aren't always appropriate. A big problem is that the actual machine code is changed between a production and debug compile. Simply calling printf could hide the bug and fudge the process into working for the debug compile but its absence would cause the production code to fail. You could argue that this is still valuable debugging information, but it's frustrating, and in such a case a debugger would be vastly more effective.

    Actually, I think we had a problem like that not too long ago (maybe on another forum), where printf was the deciding factor in whether an unrelated floating-point operation worked or didn't work.

    >Assume a new user, would you recommend printing vs. learning
    >the debugger at a newbie's stage of development.
    Yes. It's easy to understand and easy to implement without having to break from the normal process of learning the language. But I would call it a crutch and encourage learning a debugger as soon as the beginner is comfortable.
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Actually, I think we had a problem like that not too long ago (maybe on another forum)
    Yeah, I remember
    http://www.daniweb.com/techtalkforums/thread45388.html

    > As with any debugging technique, debug prints aren't always appropriate.
    Not to mention all the "my debug build works, but my release build doesn't" posts we see from time to time.
    It's considerably more interesting when it's "my release build works, but my debug build doesn't"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on debugging
    By Masterx in forum C++ Programming
    Replies: 56
    Last Post: 01-30-2009, 10:09 AM
  2. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  3. Debugging Dev C++
    By tuurb046 in forum Tech Board
    Replies: 10
    Last Post: 08-16-2007, 12:51 PM
  4. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  5. debugging directx apps
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2003, 08:56 AM