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.

Intellisense
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: We’re now going to insert a  line giving the sum of the two numbers. As  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.

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

However, in some cases, IntelliSense reports cryptic errors. Consider the below line of code: Obviously illogical. However, IntelliSense reports the error as “ ”. 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: 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”

Breakpoints
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).

This is also the opportunity to view performance data, which we’ll be looking at a later 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 measurement
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:

C in Visual Studio
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  instead of.

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