Xun Yang
Xun Yang

Reputation: 4419

how to debug c++ runtime errors

I'm using MinGW as compiler and CodeBlocks as IDE. When there's a runtime error, the program simply stop working without any error messages. Is it possible to get conventional error messages like the type of error and where it occurs?

Upvotes: 3

Views: 12477

Answers (4)

Josh
Josh

Reputation: 1052

I know this isn't a real 'answer', but I'm still new, so I can't comment on the questions yet. Anyway, same thing as Mats said, try GDB. Here is the download page http://www.gnu.org/software/gdb/

There are plenty of tutorials for using the debugger. This one is fairly good. http://www.cs.cmu.edu/~gilpin/tutorial/

If you never used a debugger, it basically runs your code line by line. You can control how far you want to proceed and where you want the code to stop to check for errors. Debuggers also keep track of other important information like variable values, variable addresses, loop counts, and so on.

If you don't want to use the debugger, you can always use print statements where you might suspect the program is crashing. For instance, if you have a function foo(), you can put a print statement on the first line of that function saying something like "in function foo". However, this method could become very tedious if you have a large piece of code. You must also remember to remove the print statements when you are finished.

Upvotes: 7

Rontogiannis Aristofanis
Rontogiannis Aristofanis

Reputation: 9063

You can use a debugger, like GDB (documentation: http://www.gnu.org/software/gdb/documentation/) Personally, I think the best way of spotting the errors is by debugging your code by yourself. A useful code snippet:

#ifdef DEBUG
 #define debug(...) do { fprintf(stderr, __VA_ARGS__); } while(false)
#else
 #define debug(...) do { } while(false)
#endif

When you suspect that you are accessing an invalid position in an array, you can try the followings:

#include <cassert>
...
int main() {
  int a[5], i;
  ...
  assert(i >= 0 && i < 5);
}

or, if you like pure C++,

#include <stdexcept>
...
int main() {
  int a[5], i;
  ...
  if(i < 0 || i >= 5) {
    throw std::runtime_error("Error: trying to access invalid memory");
  }
}

When you want to debug a recursive function, a very common source of runtime errors, add checks at the begin and at the end of your function, and maybe before or after each recursive call. For example:

int fibo(int n) {
  return fibo(n-1) + fibo(n-2); // error recursion doesn't end, stuck at infinite loop
}

A simple check at the begin of the function will help you spot easily the mistake(I know here is trivial to find why the program crashes, but in a more complex function, like DFS on a graph(DFS: http://en.wikipedia.org/wiki/Depth-first_search), it might be more tricky to find why the program causes error.

int fibo(int n) {
  if(n < 0) {
    debug("Error!");
    return -1;
  }
  return fibo(n-1) + fibo(n-2); // mistake spotted!
}

Upvotes: 3

Brut3e
Brut3e

Reputation: 549

I know using gdb is a too much steep curve but here's a link to a tutorial for gdb from where you can start debugging with gdb.!! Also if you are not familiar with compiling with terminal and using it then,

You compile the c++ program on Terminal using g++ file_name -o ouput_file_name
And then to run the program you do : ./output_file_name

(though I am a beginner) Hope it Helps..!!!

Upvotes: 2

IanPudney
IanPudney

Reputation: 6021

To determine where your error occurs, run your program in debug mode (set a breakpoint by clicking next to the line number; a red stopsign will appear). This will cause your program to pause execution on the marked line. You can then step to the next line (F7) or continue to the next breakpoint (shift+F7). This will allow you to deduce where the error occurs.

As for error codes, once your program stops working, the console will output the error code. For example, the following:

Process returned -1073741819 (0xC0000005)   execution time : 1.045 s
Press any key to continue.

indicates a memory error, in this case that I attempted to delete a non-dynamic variable. Simply google the hexadecimal code.

Upvotes: 2

Related Questions