Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 747
Xun Yang Avatar asked Feb 25 '26 12:02

Xun Yang


2 Answers

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.

like image 81
Josh Avatar answered Feb 28 '26 10:02

Josh


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!
}
like image 37
Rontogiannis Aristofanis Avatar answered Feb 28 '26 10:02

Rontogiannis Aristofanis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!