开发者

View return value?

开发者 https://www.devze.com 2022-12-16 10:33 出处:网络
Say I have the following int num = 40 + str2Int(\"30\"); Is there anyway with visual studio 2008 to tell what Str2Int is returning without ste开发者_JS百科pping into the function and going to the re

Say I have the following

int num = 40 + str2Int("30");

Is there anyway with visual studio 2008 to tell what Str2Int is returning without ste开发者_JS百科pping into the function and going to the return?


In the "auto" variable windows it will display the result of any operations you just stepped over.

Edit: Removed uncertainty over the location of this (thanks goes to Michael Burr)


Since the return value is generally in the EAX register, you put the $eax 'variable' in the watch window. When you step over the function call, what's in EAX is what that function returned.

And if you also provide the hr format symbol the debugger will show you the HRESULT or Win32 error message (like "S_OK" or "Access is denied") instead of just the raw number. It can be handy to have each ($eax and $eax,hr) in separate watch entries.

Another useful entry is $err which shows whatever GetLastError() would return (and the hr format symbol can be applied to it - or anything - as well):

$eax
$eax,hr
$err
$err,hr

Note that older versions of the VS debugger might want you to use a @ instead of a $ to start these variables, but a member of the debugger team has stated that $ is preferred to keep things in line with the "Debugging Tools for Windows" toolset (I think that support for @ is deprecated and might be removed at some point).


You can use the Visual Studio Immediate Window. This will allow you to evaluate various expressions.


cout << Str2Int("30") << endl;

Or!

cout << (num - 40) << endl;


The right way to program is to always be writing small snippets of code to test how things work. For example, if you want to study the str2int function (just as an example, as you said), create a test file just for it. Run it with different parameters, study how it works. Then, you'll be finally convinced that it's working correctly and won't be needing to step into it inside expressions. Once programmers become familiar with tools, they trust them and don't have the need to always be checking how they work.

Moreover, if this is a function you've implemented, the direct corollary of the above is to create a file with unit tests for it. Unit tests exercise the function in various ways until you can trust it's indeed working.


I haven't wanted this since I was on PowerPC, and I don't use Microsoft anything, but you probably want the register list and the assembly code view… MSVC must have those.

Stop at the instruction after the function call (which should be easy to pick out), and according to Wikipedia the return value should be in EAX/RAX. Copy the value (or learn the debugger's syntax for referencing the register) and cast to the appropriate type.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号