×

# C++ Debugging Tricks !!!

 2 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. asked 25 Nov '16, 15:56 2.6k●1●9●30 accept rate: 7%

 2 @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. answered 25 Nov '16, 16:39 5★srd091 1.6k●1●11 accept rate: 42%
 2 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. https://www.youtube.com/watch?v=USPvePv1uzE https://www.youtube.com/watch?v=y5JmQItfFck 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. answered 25 Nov '16, 16:33 537●1●8 accept rate: 27% 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. (26 Nov '16, 00:07)
 2 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. answered 27 Nov '16, 02:41 36●2 accept rate: 33%
 0 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"; DEBUG(name);  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++ link This answer is marked "community wiki". answered 25 Nov '16, 21:19 93●5 accept rate: 9% Also, I forgot comment if you feel you may forget the logic, use good naming. (25 Nov '16, 21:57) that's a bit tough to understand (26 Nov '16, 00:10) Ok, adding some explanation. (26 Nov '16, 19:28)
 0 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. answered 26 Nov '16, 19:58 0★wefgef 702●4 accept rate: 14% 2 @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. (27 Nov '16, 01:23)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,901
×123

question asked: 25 Nov '16, 15:56

question was seen: 1,827 times

last updated: 27 Nov '16, 02:41