Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / system("pause"); is the most freindly debugging tool

Author
Message
3d point in space
15
Years of Service
User Offline
Joined: 30th Jun 2009
Location: Idaho
Posted: 24th Aug 2012 23:47 Edited at: 24th Aug 2012 23:51
I use this all the time when programming a long segment of code.

Some of the code is debugged a lot easier with this simple command.
What does it do well it pauses you code at a point. Where you think there is a bug. Another advantage is to see if an array is currently in the right place.

I wonder what other people's favorite debug command is. I use it in programming apps, dark gdk as well as other programs.

Another feature is that it pulls up the dos prompt to await user to press a key.

Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps. Developed the tiled map engine seen on the showcase. Veteran for the military.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th Aug 2012 23:54
cin.ignore() is another frequent one.

TheComet

"if you don't understand recursion than you probably don't understand recursion." ~Jerico2day
Jeff032
17
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 25th Aug 2012 00:42
Can't you just set a breakpoint? It doesn't require changing any code, and you can step through things one line at a time.
Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 25th Aug 2012 00:43
Pft non BASIC languages. A combo of print and wait key is the best investment I have ever made. Except I didn't really invest in it.:/

Your signature is being eras
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Aug 2012 00:48
Yeah usually you just use the step through debugger. It's far more efficient and you have complete overview of memory, ALU, variables...

TheComet

"if you don't understand recursion than you probably don't understand recursion." ~Jerico2day
Aaron Miller
19
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 25th Aug 2012 00:53
Yeah, use breakpoints. If your editor doesn't allow you to set breakpoints, you can make calls to DebugBreak(). If you're using GCC or Clang, you can just call __builtin_trap() which should work everywhere (iirc).

Also, use assert() whenever possible.
And, if you have the ability to do so, run code analysis on your project. (I believe clang has a rudimentary version of this.)

3d point in space
15
Years of Service
User Offline
Joined: 30th Jun 2009
Location: Idaho
Posted: 25th Aug 2012 02:11 Edited at: 25th Aug 2012 02:11
Quote: "
Can't you just set a breakpoint? It doesn't require changing any code, and you can step through things one line at a time.
"

Yea you can do this but then you have to go to the debugging tool and press continue. where as pausing it you just count how many pauses occur before an error occurs.

Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps. Developed the tiled map engine seen on the showcase. Veteran for the military.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Aug 2012 04:09 Edited at: 25th Aug 2012 04:09
I'm not sure what debugger you're using, but the one in Code::Blocks allows you to resume program execution. The breakpoints have some other cool features too, for example you can set it to only break when the program passes it x number of times.

TheComet

"if you don't understand recursion than you probably don't understand recursion." ~Jerico2day
Jimpo
20
Years of Service
User Offline
Joined: 9th Apr 2005
Location:
Posted: 25th Aug 2012 04:18
Quote: "I wonder what other people's favorite debug command is."

printf!

TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Aug 2012 04:42
Another technique is using exception handlers. This here should throw a bad_alloc exception:



code lang=cpp doesn't work...

TheComet

"if you don't understand recursion than you probably don't understand recursion." ~Jerico2day
MrValentine
AGK Backer
14
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 25th Aug 2012 16:03 Edited at: 25th Aug 2012 16:04
interesting, how about creating a debug log file?

EDIT

As this will allow you to figure out exactly where your code is broken ^^

Dar13
17
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 25th Aug 2012 18:13
Quote: "Also, use assert() whenever possible."

I don't particularly like using assert, because it causes massive memory leaks when it's triggered since it doesn't allow for gracious exit. Because of that, I prefer error codes(true if successful, false is failed) or exceptions for debugging.

Most debuggers nowadays allow you to set breakpoints with certain properties, like in VS's debugger you can set it to trip only when it's been hit a certain number of times or when a variable is a specific value.

Pincho Paxton
22
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 25th Aug 2012 18:46 Edited at: 25th Aug 2012 18:46
My favourite debug code is...

Error on line 198

I don't use any debug code.

MrValentine
AGK Backer
14
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 25th Aug 2012 18:51
Quote: "Error on line 198"


is that before or after your code exceeds 198 lines of code?



RUCCUS
20
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 25th Aug 2012 20:36
NSLog("Is it here?");

-- some code --

NSLog("Maybe here...");

-- some more code --

NSLog("Where is the bug?!");

Aaron Miller
19
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 25th Aug 2012 22:20
Quote: "I don't particularly like using assert, because it causes massive memory leaks when it's triggered since it doesn't allow for gracious exit."

I tend to write my own version of assert. Likewise, you can make it conditional on whether you're debugging or not (see following snippet). And, just as well, memory leaks really don't matter considering the OS will free all the resources you've made anyway, iirc. (Unless you're targeting Windows 98...)



Quote: "how about creating a debug log file?"



----

* Use assert to make sure your code works right (even if it's a custom assert).
* Log information about what your app is doing (unless disabled) to make it easier to find where in its life cycle it crashed.
* Trigger breakpoints manually instead of crashing if you're in debug mode. I believe Windows offers an API called "IsDebuggerAttached" (I don't remember if this is actually its name or not) that you can use. So, you could write something like this:


There are probably better (read: more sophisticated) debugging methods though. Here are some #AltDevBlogADay links.
Debugging Tools
The Pain of Debuggery
When Even Crashing Doesn't Work (This article is rather advanced compared to the last two)

3d point in space
15
Years of Service
User Offline
Joined: 30th Jun 2009
Location: Idaho
Posted: 26th Aug 2012 05:21 Edited at: 26th Aug 2012 05:29
I now use breakpoints as well as system("pause"); because of this article. I think discussing this might have helped other programers out there as well.

Aaron, That debug file is pretty advanced it looks like, and too much typing to preform a debug procedure. Keep it simple stupid is the motto that I like.

Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps. Developed the tiled map engine seen on the showcase. Veteran for the military.
Dar13
17
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 26th Aug 2012 05:34
Quote: "And, just as well, memory leaks really don't matter considering the OS will free all the resources you've made anyway, iirc. (Unless you're targeting Windows 98...)"

I suppose. I just prefer a graceful crash versus a forced crash.

Aaron Miller
19
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 26th Aug 2012 07:18 Edited at: 26th Aug 2012 07:22
Quote: "That debug file is pretty advanced it looks like"

After you have that written (considering it has already been written in C, it should be pretty much portable) you can reuse it all over the place. Like so:


Likewise, my point for using assert allows you to not only clarify what types of input your code expects, but also to verify that invalid input is not passed through. Let's take another look at that DoDivide() sample:


That sort of regulation (assert) makes it easier to maintain code for (1) people who have just laid their eyes on it, like team mates, (2) yourself after you haven't seen it in a while, (3) yourself while debugging. Likewise, if a client says "it crashed!" the debug log helps, but so does the assert message because it can tell you exactly which file/line tripped the assert. (Custom-defining assert allows you to take advantage of this.) In GCC, Clang, Intel C/C++ and Microsoft Visual C++ you can also access the name of the function as well. If you were dedicated enough, you can even go through the process of performing a stack trace so you could see which functions called which other functions with which parameters prior to crashing. (However, this is a more advanced topic... But there should be libraries available for this.)

Edit: Also, there's more to KISS (I find) than in just programming. If you make it easy for yourself to manage in the future, it's worth the "suffering" you have to go through today. Imagine you've written a piece of software that thousands of people run. Would you rather interact directly with those people trying to figure out what the error is or how they got it (keep in mind, most people don't understand the type of information you need) or would you rather just have the information? All of the above suggestions help you produce more stable software, which should be your number one priority.

Michael P
19
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 28th Aug 2012 00:35
I recommend log4c (which is the C equivalent of log4j in java); logging is really useful in large systems. And ofcourse break points for really tricky bugs.

Even if you don't have a bug, do debug and trace logging to explain what is going on. log4c lets you have an XML file which defines the level of logging and where the logging goes to. So you can use this xml file to turn on or off logging to file/screen and what exactly is logged. I've even seen projects which use a custom log4c appender to write to a remote web server in real time.

Also system("PAUSE") should only be used for debugging and not left in permanently; I read that a virus could override the call and inject its own code.

Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 28th Aug 2012 00:40
Quote: "I don't use any debug code."
Must seriously suck to do that.

How could one not figure out what's going on if they cant see what values are stored at the time of the fail?

Your signature is being eras
Jeff032
17
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 28th Aug 2012 05:25 Edited at: 28th Aug 2012 05:26
Quote: "How could one not figure out what's going on if they cant see what values are stored at the time of the fail?"


At least with Visual Studio, you don't need to write "debug code" as often because if you run your program with the debugger attached, it will automatically pause when your code throws an exception or hits a breakpoint.

When it's paused, you can:
- mouse-over variables in your code to see and change their values
- change what line of code will be run next (allowing you to skip over or repeat something)
- type statements and run them, ex. to get a better view of the data, or to change something (somewhat like Interactive Mode in other languages?)
- edit your code and continue executing it from where you left off

Eclipse can probably do most of that as well.

I doubt that's what he meant, though.

Quote: "Also system("PAUSE") should only be used for debugging and not left in permanently; I read that a virus could override the call and inject its own code."


It appears that system("pause") equivalent to typing "pause" on the command line. So it will look for a file named "pause" and open it. If it happens to find another file named "pause" before it finds Windows' pause executable (because, say, its in the same folder as your program), then it will run that one instead and then bad things could happen.
Airslide
20
Years of Service
User Offline
Joined: 18th Oct 2004
Location: California
Posted: 29th Aug 2012 04:06
I think "assert" is one of the more useful macros/functions/whatever-it-is-in-your-favorite-language out there. Personally I don't mind the lack of a "graceful" exit if only because any assert-raised error should be considered a programmer error, and crashing the program is appropriate in that case.

That said, I also like the error-code (or in Objective C's case, error-object) methodology better than exceptions.

My favorite debugging tool is...well, the debugger. Both VS and Xcode have very nice offerings in this regard (the VS debugger for C# is fantastic).
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 29th Aug 2012 06:12
Quote: "NSLog("Is it here?");

-- some code --

NSLog("Maybe here...");

-- some more code --

NSLog("Where is the bug?!");"


That's what I do when working on my iPhone app. Also, I spend most of my working day in PHP, so I just use echo() and vardump() everywhere (usually with 'exit' straight after)


Senior Developer - CBS Interactive Music Group
Airslide
20
Years of Service
User Offline
Joined: 18th Oct 2004
Location: California
Posted: 29th Aug 2012 09:13 Edited at: 29th Aug 2012 09:14
Surely you mean:

Quote: "NSLog(@"Constant string prefixed with @ which I often forget...till the compiler yells")"


I too tend to log things when my data is looking funny, or when an issue only happens rarely, but for the most part I breakpoint and use the debugger to look at variables where possible.

Login to post a reply

Server time is: 2025-05-18 07:34:37
Your offset time is: 2025-05-18 07:34:37