Visual Studio/Intellisense and debugging

One of a programmer’s most arduous tasks is to debug a program – repeatedly testing it to make sure that it’s free from errors. Here, we’ll take a brief look at what Visual Studio can do to help the developer on that.

IntellisenseEdit

It is a fuzzy-logic system used to predict code as it is typed. For example, let’s say that your code looks like this:

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int a, b;
	cout << "Enter the two numbers\n";
	cin >> a >> b;
}

We’re now going to insert a cout line giving the sum of the two numbers. As cout is entered, Visual Studio immediately displays a box suggesting possible results for what you have just entered.
 
Tab to accept the given output.
This alone is very powerful, as it saves developers’ time. Instead of manually having to write every command manually, you can just type a part of it and Visual Studio will suggest what you’ve thought.

Before Visual Studio 2012, Intellisense for C++ had to be invoked manually by pressing CRTL+J:
 

Error detectionEdit

Another very useful feature of Intellisense is the ability to detect errors before runtime. Some of them would be very trivial:
 

In the above example, if is misspelled as iff

However, in some cases, IntelliSense reports cryptic errors. Consider the below line of code:

3 = a;

Obviously illogical. However, IntelliSense reports the error as “expression must be a modifiable lvalue”. While some of them would be very hard to diagnose, IntelliSense reports the line numbers wherever possible, and hence you can use that to pinpoint the problem. But IntelliSense is not all rose and gold. For example, consider this line:

cout << 0/0;

This line is obviously not possible. But IntelliSense does not catch it! You’ll need to build it before the error comes out: “C2124: divide or mod by zero”

BreakpointsEdit

One of the most fundamental components of debugging is setting breakpoints. Simply put, they allow you to view the value of the program’s variables at any point of the program’s execution and is very useful when a piece of code is having problems but you’re not sure where. Setting breakpoints is easy: go to the line which you want to stop the program execution, and press F9. Alternatively, click on the left bar just inside the code window. A circle will appear, which means that a breakpoint has been set on that line.
 
Now debug the program. When the specified line is reached, the program stops. Here’s how Visual Studio looks during this step for an example program:
 
It is possible to view variables’ values by using the Locals and Autos windows, or by simply hovering on top of the said variable (both are demonstrated in this screenshot).

The Locals and Autos windows, through both displaying variable values, do not target the same areas and hence target different variables.

This is also the opportunity to view performance data, which we’ll be looking at a later stage.

It should be noted that if you pause the execution of the program by using the Pause option in the toolbar, you’ll also reach the break stage.

If some cases, the variables cannot be viewed, and Visual Studio will report that they are “optimised away and not available”. This can happen for a variety of reasons, including but not limited to heap corruption, garbage collection and so on.

Performance measurementEdit

Newer versions of Visual Studio include features to track and analyse the usage of various components like the CPU and memory by the program.
 
Now once a breakpoint is hit or the program execution is paused, the memory usage corresponding to the time the snapshot was taken can be viewed:
 
This requires that you take regular snapshots so that you can compare memory consumption and usage across two different times of the program. It is also possible to view CPU process information up to that point:
 

Note that the relevant options must be enabled explicitly, as they create additional CPU overhead and may affect the program depending on its intended task.

C in Visual StudioEdit

Despite the fact that Visual Studio C++ is primarily meant to support C++, Visual Studio has fairly strong support for the C Programming language. The problem however is that there's no implicit option to do so.

To work around, create an empty project file. Then, in Solutions Explorer, add a new C++ file, but set the file extension as .c instead of .cpp.

IntelliSense support is available, and it's not C++. To prove it, cout will no longer work as it's not part of the C specification; you'll need to use printf.