Debugging code tips in Visual Studio

Struggling with debugging in Visual Studio? Here are some debugging code tips in Visual Studio

Three key questions to ask yourself while debugging

  1. Where is your error? – Find the line of code which has the first error
  2. What is your error? – Read the error message carefully, copy and paste the error message into google if needed
  3. What is causing the error? – Go to the line of code, find and fix the mistake in the code

If you have multiple errors in your program, fix the first few errors and work through the list from the start to the bottom. Keep re-compiling the project as generally the number of errors will be reduced substantially once the first few errors are fixed. The advantage of using visual studio is that you can look for red squiggle in your code which means that Visual Studio has detected an syntax error and won’t compile the code until you correct it.

Error types you will encounter

Syntax Errors

What happens – The code does not compile because the rules of the C++ programming language have not been followed. Work through compiler errors from the first few to the bottom.

How to avoid these errors – Don’t write code really fast as you will save a lot of time of not having to debug a long list of compiler errors

Logic errors 

What happens – The code compiles, but does not work as expected. These errors are the hardest to find.

How to avoid these errors – The way to combat is to test, test and test your code. Try to brainstorm and think of all possible scenarios for your code and test for them.

Runtime errors

What happens – The code compiles, but the program performs an illegal operation whilst running. Examples include divide by zero, going out of bounds for an array.

How to avoid these errors – Use try/catch statements to handle any errors that exceptions. Carefully read any runtime error messages that appear. These errors can be confusing so researching the error on Google and Stackoverflow can give an indication of the problem.

How to use the debugger in Visual Studio 

Start debugging [F5] – Complies the code and runs till breakpoint

Set breakpoint[F9] – Toggles break point on the line in the cpp file

Step into [F11] – Exectues one line in the program. If that line is a function call, it jumps to the first line of the function

Step out[Shift + F11] – Continue execution until the current function returns or until a breakpoint in the function is reached

Step over[F10] – Executes one line in the program. If that line is a function call, the entire function is executed.

Share this post