The wrong answer-this time, it returns the value 1. While hovering over a variable in the source code.)Ĭontinuing execution of the program demonstrates that, in fact, we still get (You can also do this when the variable's value pops up Let's do that with the value of sum, and make sure that it returns the correctĪll you need to do is click on in the Value column of the auto or watch windowĪnd change the value. Studio conveniently makes it easy to change any value in memory. Obviously, one way of doing this would be to pass in a new value.
![watch window in visual studio shortcuts watch window in visual studio shortcuts](https://abhijitjana.files.wordpress.com/2010/10/methodcall4_thumb.png)
We'll deal with that in a bit.įirst, let's test and make sure that the function works if we do get the right Now that we're in the compute_average function, and we know that the valueīeing returned is waayyy too big, we can check the arguments to theįunction. Watch out for early returns from a function within a loop, for instance.
#Watch window in visual studio shortcuts code#
Be careful, though, that you run to a part of the code that will actually be executed. Please imagine 15-20 calls to compute_average that all work correctly, takingĪny time you want a throwaway breakpoint or want to avoid single-stepping through a lot of code. (Of course, we could just put aīreakpoint in compute_average for the purpose of making this example sensible,
![watch window in visual studio shortcuts watch window in visual studio shortcuts](https://itenium.be/assets/blog-images/debugging.png)
Step-out trick, to get into compute_average. Going back to our sample program, we can use run-to-cursor rather than the You can place a breakpoint at the start of that path and use run-to-cursor to It is most useful when you have a piece of code that is calledįrequently from different places, and you only care about one code path. Immediately cleared once it's hit nevertheless, it's a very convenientįeature. In effect, run-to-cursor is just a shortcut for setting a breakpoint that is You want to be, getting to it requires stepping multiple times, and you get Step-step-step-step-step-oh-no-I-stepped-over-it problem, where you know where Run to cursor is a great way of avoiding the Most return values are also stored in the EAX register-you can look in EAX to find the return value, if you need it. Whenever you want to see a return value! Note that you the autos window will eventually erase the return value as you execute code, so be sure to check your return value immediately. This is rarely true in the general case.)įortunately, the autos window has good courtesy to display the result of a (Obviously, in this case, the program prints out the value, so all is not lost. Sometimes, programmers write code like this, just to work around the issue:Ĭomputed_avg = compute_average( d.getSum(), d.getCount() ) Whose result isn't stored anywhere, and you really want to know what it One of the most frustrating parts of debugging is when you have a function call Use the Auto Window to See Result of Functions The function and set a temporary break point (or use run to cursor).Ģ. Step out provides a quick way of getting into a function without having to find But when doing so would require ignoring hundreds of calls to that function to find the one you want, Obviously, there are times when it makes more sense to just set a breakpoint in Repeat until you've drilled down into the function you want. But now you can step in again, to go to the next function. Once you step out of the current function, you're taken back to the originalīreakpoint. Shift-F11 to step out of the current function and return to the next call. To get out of getSum and getCount, but with Visual Studio, you can quickly hit
![watch window in visual studio shortcuts watch window in visual studio shortcuts](https://www.fmsinc.com/microsoftaccess/Module-Coding-Tools/debugging/view-menu.png)
Will both be called before compute_average. Once the debugger hits it, then step into the function. Of course, put a breakpoint inside compute_average, but what if it were calledįrom several places? Visual Studio has a very convenient feature of itsĭebugger, that will allow you to step into compute_average very quickly. Were to use a debugger, and you wanted to step into compute_average, you could, So you've written a function call, like so:Īnd the compute_average function is not returning the correct value. Std::cout << compute_average( d.getSum(), d.getCount() ) << std::endl
![watch window in visual studio shortcuts watch window in visual studio shortcuts](https://i.stack.imgur.com/aFwoy.png)
Int getCount() ĭouble compute_average( int sum, int count ) Each of the following examples use this code, which contains several bugs:įor ( std::vector::iterator itr = _values.begin(), end = _values.end() itr != end ++itr )