Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this Quickstart, you explore how to use the Visual Studio debugger to you debug your native C++ code. This article provides a quick way to learn some of the basic features for working in the debugger.
If you don't have Visual Studio, you can install the free trial version from the Visual Studio Downloads page.
Create a new project
Start by creating a new project, so you have some code to check in the debugger.
Open Visual Studio and create a new project.
If the Start Window isn't open, select File > Start Window.
In the Start Window, select Create a new project.
In the Create a new project window, set the filters and search for a project template.
In the Search for templates box, enter empty.
Expand the Language dropdown and select C++.
In the list of results, select the Empty Project template for C++, and then select Next.
If you don't see the Empty Project template for C++, select the Continue without code option.
Select Tools > Get Tools and Features..., which opens the Visual Studio Installer.
In the Installer window, select the checkbox for the Desktop development with C++ workload, and then select Modify.
Your Visual Studio installation updates to include the selected C++ workload.
After the update completes, select File > Start Window, and follow the previous instructions to select the required template.
Configure the new project:
Enter a Project name and Solution name, or use the default values.
Use the default Location or select browse (...) to create the project in a different folder.
Select Create.
Visual Studio creates your new project and adds the project files in the specified folder.
Add a project file
Add a code file so you have content to test in the debugger.
In the Solution Explorer pane, right-click your <project-name> and select Add > New Item.
If you don't see the Solution Explorer pane, select View > Solution Explorer.
In the Add new item dialog, enter the file name MyDbgApp.cpp, and then select Add.
The file opens in the code editor.
Add the following code to the new file.
#include <list> #include <iostream> using namespace std; void doWork() { list <int> c1; c1.push_back(10); c1.push_back(20); const list <int> c2 = c1; const int &i = c2.front(); const int &j = c2.front(); cout << "The first element is " << i << endl; cout << "The second element is " << j << endl; } int main() { doWork(); }Save your file changes with the Ctrl+S keyboard shortcut.
Set a breakpoint
A breakpoint is a marker that indicates where Visual Studio should suspend your running code. When a breakpoint is set, you can take a look at the current values of variables, examine the behavior of memory, and check if a specific branch of code is running. Breakpoints are the most basic feature in debugging.
To set a breakpoint, locate the call to the
doWork()function in the code, and then select in the left gutter on the same line.When the breakpoint is set, a red dot displays in the left gutter on the corresponding line in the code.
Start debugging by using the F5 keyboard shortcut (or select Debug > Start Debugging).
Code execution pauses immediately before the call to the
doWork()function.The debugger pauses where you set the breakpoint. A yellow arrow identifies the statement where the debugger pauses app execution.
Tip
When you're debugging memory-related failures in your C++ code, you can also use breakpoints to inspect address values (look for
NULL) and reference counts.
Set conditional breakpoints
If you set a breakpoint in a loop or recursion, or if you have many breakpoints that you frequently step through, use a conditional breakpoint.
This approach helps ensure your code is suspended only when specific conditions are met. A conditional breakpoint can save time and also make it easier to debug issues that are hard to reproduce.
Walk through your code
Visual Studio provides several different ways for instructing the debugger to continue with app execution. The following example shows a useful command for walking through your code.
While the code is paused at the breakpoint, hover over the statement c1.push_back(20) until you see the green Run to click (Run execution to here)
icon. Select the icon.
While the code is paused at the breakpoint, hover over the statement c1.AddLast(20) until you see the green Run to click (Run execution to here)
icon. Select the icon.
The app continues execution, calling doWork, and pauses on the line of code where you selected Run to click.
Common keyboard commands used to step through code include F10 and F11. For more in-depth instructions, see Overview of the Visual Studio debugger.
Inspect variables in a data tip
You can inspect the state of your variables by using the data tip feature.
In the current line of code (marked by the yellow execution pointer), hover over the
c1object to see the data tip.
The data tip shows you the current value of the
c1variable and allows you to inspect its properties.When you're debugging, if you see an unexpected value for a variable, you probably have a bug. The bug could be in the code that makes the call to the variable or in the previous line.
Expand the data tip to look at the current property values of the
c1object.To continue checking the value of
c1as the code executes, select the pin
icon on the data tip.The pin action keeps the data tip open. As the code executes, the value in the pinned tip changes to show the current state of the watched object.
You can move the pinned tip to any location in the IDE, so it doesn't prevent you watching other items while debugging.
Enable hot reload while debugging
While you're debugging, if you see a change that you want to test in your code, you can use the Hot Reload feature (previously called Edit and Continue).
Select the second instance of the
c2.front()statement, and change the code toc2.back().Select F10 a few times (or select Debug > Step Over a few times) to advance the debugger and execute the edited code.
F10 advances the debugger one statement at a time, but steps over functions instead of stepping into them (the code that you skip still executes).
For more information about the feature and limitations, see Configure Hot Reload.