C++ Debugging Tricks !!!

So, I am having problems debugging my code in C++. It takes about like 1/2 hour to debug and then I realize how silly the mistake was. I am new to C++ so please help.

1 Like

Try and have a look at GDB; though it is a bit awkward for beginners but is good to use and saves time. If you are not comfortable with this nothing to worry, practice more problems and you will get good with time.
You may refer to these links from cs50 courses for GDB.

EDIT: Also try breaking program into functions/using inbuilt functions.It helps in generating shorter understandable code which makes debugging a lot easier. Whenever I am stuck, I decompose the program into functions. It generally helps.

1 Like

@mathecodician These are some things that you can do, first note down your frequent mistakes and find out the reason, you tend to make them and next time while debugging see through the list, and check whether you have done same mistakes or not.
Start writing short comments in your code, this will reduce your effort significantly while debugging. Always try to code simple so that you can reduce your implementation mistakes.

The main thing is practice, as you expose yourself to more and more problems, you’ll learn all the little tricks that they can throw at you, which will definitely improve your ability to write clean, foolproof code.

1 Like

Hmm @mathecodician I suggest using this for logic error detection, but you have to take care of syntax and infinite loops yourself. Use this macro that I use regularly, if you suspect some variable causing havoc.

#define DEBUG(x) std::cerr << #x << ": " << x << '\n';

Moreover, sometimes GDB tricks are also useful so you could try that also, if you please.

I might also add some more information if you found this useful.

You may also think of using something like

#define DEBUG(x) std::cerr << "In " << __FILE__ << " at line " << __LINE__ << #x << ' = ' << x << std::endl;

Now, I didn’t use __TIME__ as I think it’s useless for me and __func__ works only in C++11 and above.
Keep in mind that for arrays you need a loop to debug, so this just doesn’t work.

The first macro I gave prints the value of a given variable. If you have defined that macro earlier, and you try calling it on a variable, example:

std::string name = "value";

will print name : value
On the other hand, using the second macro on the variable name, will give you this as output assuming the file was test.cpp and your assignment was on line 20, you’ll get:

In test.cpp at line 20 name = value

The #x is used to print the name of your variable, FILE is the name of your C++ file, LINE is the number of the line at which the variable was last changed. And then I just display the value, I’m displaying it on the error stream, but you can also display it on cout if you wish to, but then you’ll have to take care to define it to be used only when not final submission.

@mathecodician feel free to try to use the second macro. Example: http://ideone.com/YfzMd4

Try this, it’s really helpful sometimes.
GDB is much more complicated, so stick to this. I do not recommend IDEs because the centres may not have your IDE installed, so use a terminal like Bash/Zsh/PowerShell and Vim/NotePad++

Also, I forgot comment if you feel you may forget the logic, use good naming.

I also think it’s a bit awkward for me, currently I don’t understand it but I think it might be useful later on.

that’s a bit tough to understand

Ok, adding some explanation.

Besides practicing more, which is the obvious advice, people also mentioned GDB. Unfortunately though, GDB is awful to use, especially if you are a beginner. And to be honest it’s not so great even if you master using it.

I recommend you to learn Visual Studio. It has by far the best debugger out there - Microsoft has done a great job. The good news is that Visual Studio is now available on Linux and Mac too. You can download it here.

@mathecodician just adding a point; visual studio debugger is good but it is not available during competitions such as icpc. So it will help to debug at home or at codechef but might not be available for the competitions. So please confirm the rules if you are planning to give any such competitions as getting good in debugging code by printing variables/GDB will be a more helpful skill to learn in that case.

1 Like

I would recommend you to use more of STL containers like vector, std::array, stack, list, map, set instead of using normal arrays. Because in gdb, p command works better with STL containers, unless C-like arrays have constant size at compile time.

I would recommend you to learn at least minimal commands that makes debugging easier.

b - breakpoint

c - continue

n - next line

s - step into

p - <“variable name”>.

Most of time c is underrated. But it is very useful command. Try to use it as much as possible. Because we don’t really need the status at every line.

1 Like