{"id":16115,"date":"2017-06-23T14:03:13","date_gmt":"2017-06-23T21:03:13","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=16115"},"modified":"2019-02-18T17:48:26","modified_gmt":"2019-02-18T17:48:26","slug":"c-tutorial-debugging-overview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/c-tutorial-debugging-overview\/","title":{"rendered":"C++ Tutorial: Debugging Overview"},"content":{"rendered":"<p>Welcome to the <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/tag\/c-tutorial\/\">C++ Tutorial series<\/a>. In this article, we explore debugging concepts and do some hands-on debugging with Visual Studio 2017.<\/p>\n<p>One of the most useful features of an IDE \u00a0is the debugger. Under a debugger, you can step through code as it is running, examining the values of variables, structures, program flow, screen output, and other elements of the application for errors. It is slower than running the program outside of the debugger, but vital to understanding the inner workings of an application.<\/p>\n<h2>When Debugging Helps<\/h2>\n<p>Even the best developers make mistakes. That is why the best developers know their way around a debugger and have sound strategies for diagnosing and fixing code issues even when the code belongs to someone else.<\/p>\n<p>You may need to debug when:<\/p>\n<ul>\n<li>Code crashes randomly, stopping the application and losing data<\/li>\n<li>Results are wrong or different from previous versions<\/li>\n<li>Divide by zero<\/li>\n<li>You need to optimize code based on profiling results<\/li>\n<li>Feature does not work as expected (selecting \u201cprint\u201d produces a garbled page)<\/li>\n<li>User interface elements are in the wrong place, wrong size, have wrong labels\u2026<\/li>\n<li>Network communication is not working<\/li>\n<li>Game enemy keeps running into the corner and dying (AI issue)<\/li>\n<li>Multi-threaded code is deadlocking<\/li>\n<li>Component fails to load<\/li>\n<li>Customer files a bug in your feature area<\/li>\n<li>You need a better understanding of how code works under the hood<\/li>\n<\/ul>\n<p>With practice, you will get better at diagnosing issues, isolating behavior, and fixing code. It takes time.<\/p>\n<h2>Debugging Hello, World<\/h2>\n<p>Time for some hands-on experience with the Visual Studio debugger. We will use the <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2017\/05\/31\/cpp-tutorial-hello-world\/\">Hello, World program from the first tutorial<\/a> and then modify it to include a variable we can inspect (and change).<\/p>\n<ol>\n<li>Load the Hello, World project from the <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2017\/05\/31\/cpp-tutorial-hello-world\/\">Hello, World tutorial<\/a>.<\/li>\n<li>Press <strong>F10<\/strong> to start debugging. Visual Studio will rebuild your project if any changes were detected. Debugging will begin with the first statement (indicated by the yellow arrow, line 4), the open brace of function main():\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-2.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-2.jpg\" alt=\"Visual Studio debugger is running\" width=\"550\" height=\"320\" class=\"alignnone size-full wp-image-16125\" \/><\/a><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-2.jpg\"><\/a>\nThis statement has not yet been executed.<\/li>\n<li>Press <strong>F10<\/strong>. The yellow arrow should be pointing to the Hello, World output statement (line 5):\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-3.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-3.jpg\" alt=\"Next statement will print Hello World\" width=\"550\" height=\"320\" class=\"alignnone size-full wp-image-16126\" \/><\/a><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-3.jpg\"><\/a>\nTo the right of the statement, you can see the previous statement took less than one millisecond to execute. You can use it to identify bottlenecks and optimize, subjects for another day.<\/li>\n<li>Hover the mouse cursor over \u201cHello, World!\u201d. Visual Studio will pop up a visualizer for the constant:<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-4.jpg\"><img decoding=\"async\" width=\"692\" height=\"228\" class=\"alignnone size-full wp-image-16135\" alt=\"Visualizing a string (array of characters) in C++\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-4.jpg\" \/><\/a>In Visual Studio, visualizers help you understand what a constant, variable, class or other element \u201clooks like\u201d. A string constant is an array of characters. If you click the + to expand the view, the string will be shown as an array:<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-4b.jpg\"><img decoding=\"async\" width=\"550\" height=\"270\" class=\"alignnone size-full wp-image-16145\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-4b.jpg\" \/><\/a><\/li>\n<li>Press <strong>F10<\/strong> to execute the output statement. Look at the output console window to see \u201cHello, World!\u201d printed out:<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-6.jpg\"><img decoding=\"async\" width=\"440\" height=\"290\" class=\"alignnone size-full wp-image-16175\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-1-step-6.jpg\" \/><\/a><\/li>\n<li>Press <strong>F10<\/strong> to execute the return statement and press F10 one more time to exit main() (returning 0)<\/li>\n<\/ol>\n<p>Congratulations, you have just debugged your first C++ application.<\/p>\n<h2>Integrated and Stand-Alone Debuggers<\/h2>\n<p>Some debuggers are stand-alone and some are integrated into a development environment.<\/p>\n<p>A<strong> stand-alone debugger <\/strong>exists independent of other development tools. Because it is independent, it might have a different user interface, might support a broader set of languages, and might need to be told which program to launch for debugging or which running program to attach to for debugging. <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows-hardware\/drivers\/debugger\/getting-started-with-windbg\">WinDBG<\/a> and\u00a0<a href=\"https:\/\/www.gnu.org\/software\/gdb\/\">GDB<\/a>\u00a0are popular stand-alone debuggers.<\/p>\n<p>An <strong>integrated debugger<\/strong> is part of a development tool, usually alongside other useful development tools in the IDE. With an integrated debugger, you do not need to switch from editor to debugger to diagnose issues. The user interface is consistent, languages supported by the IDE are usually supported by the integrated debugger, and it is already configured for common scenarios. Visual Studio has an integrated debugger that works with all Visual Studio programming languages and their associated libraries.<\/p>\n<h2>Common Features<\/h2>\n<p>Most debuggers share a common set of features. Here are the features you need to know (and how to use them with C and C++ code in Visual Studio).<\/p>\n<p><strong>Start debugging<\/strong><\/p>\n<p>To debug, you need to start your app with the debugger attached to the process.<\/p>\n<p>In Visual Studio, press <strong>F5<\/strong> or select <strong>Debug<\/strong> | <strong>Start Debugging<\/strong>. Your code may need to be rebuilt.<\/p>\n<p>Sometimes, in the heat of debugging, you may forget whether you are in edit mode or actively debugging an application. Visual Studio provides clues. In edit mode, the bar at the bottom of the editor is blue.<\/p>\n<p>In debugging mode, \u201c(Debugging)\u201d appears in the title bar and the bar on the bottom of the screen is orange. And unless you\u2019ve made configuration changes, there are debugging and diagnostic panels in the editor.<\/p>\n<p><strong>Setting a breakpoint <\/strong><\/p>\n<p>Breakpoints are useful when you know the line of code or the section of code that you want to examine in detail. By setting a breakpoint, you tell the debugger to pause when execution hits a selected line of code so you can inspect values, examine output, change program flow, override values, and perform other diagnostic tasks.<\/p>\n<p>In Visual Studio, you set a breakpoint by clicking in the margin to the left of a line of code, by pressing <strong>F9<\/strong>, or by selecting <strong>Debug<\/strong> | <strong>Toggle Breakpoint<\/strong> on the menu. When in debug mode, you can <strong>Run to Click<\/strong>, to advance the debugger to the line of code where you clicked by hovering over over a line of code until the <strong>Run to Click<\/strong> (Run execution to here) button appears.<\/p>\n<p><strong>Step into code<\/strong><\/p>\n<p>Stepping into code advances the app execution into the details of the function. You will see the underlying function code in detail and is helpful when you are trying to identify where a bug might be hiding.<\/p>\n<p>In Visual Studio, press <strong>F11<\/strong> to step into code. If you press this while not debugging, the debugger will start and execution will begin at the first line of your code.<\/p>\n<p><strong>Step over code<\/strong><\/p>\n<p>Stepping over code advances the debugger without exposing function details. The code still executes, but execution advances in a single &#8220;leap&#8221;, avoiding the implementation. It is a good way to skip over code that you&#8217;re not interested in, so you can quickly get to code that you are more interested in.<\/p>\n<p>Press <strong>F10<\/strong> in Visual Studio to step over code.<\/p>\n<p><strong>Inspect variables<\/strong><\/p>\n<p>Inspect the current type and value of variables in scope to find incorrect data and better understand program behavior. You might be able to see variables used in the previous few lines of code (called automatic or <em>auto<\/em> variables) and <em>local<\/em> variables (variables currently in scope, often including the implicit \u201cthis\u201d pointer when inside a C++ object).<\/p>\n<p>The Visual Studio debugger displays both <strong>Autos<\/strong> and <strong>Locals <\/strong>windows while debugging. You can see the value of an individual variable by hovering the mouse cursor over it.<\/p>\n<p><strong>Modify a variable<\/strong><\/p>\n<p>Sometimes it is useful to see what happens when variables have different values while debugging. For example, your application might control an automatic lawn sprinkler that turns off when rain is detected; you might set the &#8220;isRaining&#8221; flag manually regardless of whether it is actually raining to make sure the sprinklers turn off and on as desired.<\/p>\n<p><span>Values can be C and C++ expressions. If you use operators that modify values (like post-increment) or call a function, it can change the value of other variables or otherwise affect the state of your application.\u00a0<\/span><\/p>\n<p>In Visual Studio, t<span>he variable windows, <\/span><strong>Autos<\/strong><span>, <\/span><strong>Locals<\/strong><span>, and <\/span><strong>Watch,<\/strong><span> display the values of certain variables during a debugging session. The <\/span><strong>QuickWatch<\/strong><span> dialog box can also display variables. When the debugger is in break mode, you can use the variable windows to edit the values of most variables that appear in these locations.<\/span><\/p>\n<p><strong>Examine the call stack<\/strong><\/p>\n<p>The call stack shows the order in which methods and functions are called (like function x calling function y which in turn calls function z). This is a good way to understand the execution flow of an app and helps answer the question \u201cwhere am I in code and how did I get here?\u201d<\/p>\n<p>The Visual Studio debugger automatically shows the <strong>Call Stack<\/strong>.<\/p>\n<p><strong><\/strong><strong>Change execution flow<\/strong><\/p>\n<p>By changing execution flow, you change which statement will be executed next. This is helpful to force execution flow down a certain code path or to re-execute a block of code after setting variables to different values.<\/p>\n<p>With the Visual Studio debugger paused on a line of code, use the mouse to grab the yellow arrow pointer on the left and move the yellow arrow pointer to a different point in the code execution path. Then you use F5 or a step command to continue running the app.<\/p>\n<p><strong>And more\u2026<\/strong><\/p>\n<p>Learn more about <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/debugger\/debugger-feature-tour\">general debugging features in Visual Studio<\/a> and then dig a little deeper into <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/c-debugging-and-diagnostics\/\">C++ debugging and diagnostics<\/a>. If you are using another debugger, consult the documentation to learn what features are supported.<\/p>\n<h2>A More Advanced Example<\/h2>\n<p>We have not yet looked at C++ types, but let\u2019s add an integer variable and output statement to Hello, World and see how we can visualize and modify variable values.<\/p>\n<ol>\n<li>Load the Hello, World project from the <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2017\/05\/31\/cpp-tutorial-hello-world\/\">Hello, World tutorial<\/a>.<\/li>\n<li>Add the following two statements before return 0 (line 6):\n<code>\nint x = 47;\nstd::cout &lt;&lt; x &lt;&lt; std::endl;\n<\/code>\nYour code should look like this:<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-2-step-2.jpg\"><img decoding=\"async\" width=\"400\" height=\"150\" class=\"alignnone size-full wp-image-16185\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-2-step-2.jpg\" \/><\/a><\/li>\n<\/ol>\n<ol start=\"3\">\n<li>Right-click on line 7 and select <strong>Run To Cursor<\/strong> (<strong>CTRL-F10<\/strong>). Hover the mouse over the variable x to see its current value. It can also be seen in the Autos window (both are shown below):<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-2-step-3.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-2-step-3.jpg\" alt=\"\" width=\"500\" height=\"185\" class=\"alignnone size-full wp-image-16155\" \/><\/a><\/li>\n<\/ol>\n<ol start=\"4\">\n<li>Press <strong>F10<\/strong> and check the output window. You should see \u201c47\u201d under \u201cHello, World!\u201d. Does the output work for a different number? Let\u2019s find out.<\/li>\n<li>Drag the yellow arrow to line 7 (or move the cursor to line 7, right-click and select <strong>Set Next Statement<\/strong>).<\/li>\n<li>Modify the value of x. Hover over <strong>x<\/strong> on line 7 and then click on <strong>47<\/strong> when it appears to edit. Change it to <strong>4350<\/strong> and then click <strong>enter<\/strong>.<\/li>\n<li>Press <strong>F10<\/strong> to output the value. Check the output console; you should see the following:<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-2-step-7x.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/t2-demo-2-step-7x.jpg\" alt=\"\" width=\"443\" height=\"290\" class=\"alignnone size-full wp-image-16205\" \/><\/a><\/li>\n<\/ol>\n<ol start=\"8\">\n<li>You can stop debugging any time by selecting <strong>Debug<\/strong> | <strong>Stop Debugging<\/strong> (SHFT+F5). Do so now.<\/li>\n<\/ol>\n<p>Congratulations again, you just modified a variable and changed program flow in a running application.<\/p>\n<h2>Review<\/h2>\n<p>In this C++ tutorial, you learned the basics of debugging including when you might need to debug (like when a function is not providing the expected results), the difference between stand-alone and integrated debuggers (integrated debuggers are part of an IDE and might be easier to use), and common debugger features. You also debugged a simple and a more complex application, changing values and modifying execution flow.<\/p>\n<p>Want more? Find additional <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/tag\/c-tutorial\/\">C++ tutorials<\/a> and <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/tag\/debugging\/\">debugging insights<\/a> on this blog.<\/p>\n<p>If you have any feedback or suggestions for us, please reach out. We can be reached via the comments below, via email (<a href=\"mailto:ebattali@microsoft.com\">ebattali@microsoft.com<\/a> or <a href=\"mailto:visualcpp@microsoft.com\">visualcpp@microsoft.com<\/a>) and you can provide feedback via Help | Report A Problem in the product, or via <a href=\"https:\/\/developercommunity.visualstudio.com\/spaces\/8\/index.html\">Developer Community<\/a>. You can also find us on Twitter (<a href=\"https:\/\/twitter.com\/visualc\">@VisualC<\/a>) and Facebook (<a href=\"https:\/\/www.facebook.com\/msftvisualcpp\">msftvisualcpp<\/a>).<\/p>\n<hr \/>\n<p><em>Thanks to Kate Gregory &#8211; Partner, Gregory Consulting Limited; and James McNellis &#8211; Microsoft Senior Software Development Engineer, for allowing us to use some of their video lectures, demonstrations, and content. Thanks also to Gerry O\u2019Brien, Microsoft Learning Experience, for curriculum design and assistance.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the C++ Tutorial series. In this article, we explore debugging concepts and do some hands-on debugging with Visual Studio 2017. One of the most useful features of an IDE \u00a0is the debugger. Under a debugger, you can step through code as it is running, examining the values of variables, structures, program flow, screen [&hellip;]<\/p>\n","protected":false},"author":264,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[313],"class_list":["post-16115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-c-tutorial"],"acf":[],"blog_post_summary":"<p>Welcome to the C++ Tutorial series. In this article, we explore debugging concepts and do some hands-on debugging with Visual Studio 2017. One of the most useful features of an IDE \u00a0is the debugger. Under a debugger, you can step through code as it is running, examining the values of variables, structures, program flow, screen [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/16115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/264"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=16115"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/16115\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=16115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=16115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=16115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}