Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio expression containing a term named "by" cannot be evaluated in the watch window

Consider my C++ code below:

int _tmain(int argc, _TCHAR* argv[])
{
    int by = 10;
    printf("%d\n", by);

    int bx = 20;
    printf("%d\n", (by + bx));

    return 0;
}

which works fine. The funny thing is with the "by" variable. If I try to add a watch for a simple expression that contains by, the result will be CXX0030: Error: expression cannot be evaluated.

For example, on a breakpoint on return 0, if I add the following watches I get the results mentioned:

by : 10
bx : 20
by + 5 : CXX0030: Error: expression cannot be evaluated
bx + 5 : 25
by + bx : CXX0030: Error: expression cannot be evaluated
(by) + bx : 30
by + (bx) : CXX0030: Error: expression cannot be evaluated
bx + (by) : CXX0014: Error: missing operrand

This happens on VS2010, VS2008 on multiple computers.

So, more out of curiosity, what is happening with "by"? Is it some kind of strange operator? Why doesn't bx get the same treatment?

(I've tried google on this but it is quite difficult to get some relevant hits with terms like "by")

like image 354
Andrei Pana Avatar asked Jan 10 '11 17:01

Andrei Pana


People also ask

How do I use the Watch window in Visual Studio?

It's available from Debug | Windows | Watch | Watch 1 or Ctrl + Alt + W + 1. There are 4 watch windows in Visual Studio, which you can use in different contexts (Watch 1, Watch 2, etc.). Any expression can be entered into the watch window. The same rules apply to expressions as to code.

How do I fix exception thrown in Visual Studio?

When the debugger breaks, it shows you where the exception was thrown. You can also add or delete exceptions. With a solution open in Visual Studio, use Debug > Windows > Exception Settings to open the Exception Settings window. Provide handlers that respond to the most important exceptions.


2 Answers

Intrigued by this, I did some digging. From this link, we see that the native C/C++ expression evaluator in the debugger handles Assembly-Language expressions; following the Assembly-language link, we discover that BY is short for BYTE in an an Assembly-Language expression. So just another Microsoft cock-up

like image 183
TonyK Avatar answered Nov 06 '22 22:11

TonyK


What you're seeing here is the C++ Expression Evaluator's implementation of the BY operator. Your use of the expression BY is being interpreted as an operator instead of a local variable.

Reference: http://msdn.microsoft.com/en-us/library/56638b75.aspx

A lot of discussion has gone into whether or not this behavior is a bug or by design. Unfortunately that can only be truly answered by the people who implemented the feature. As dissatisfying as it is for this scenario there could be valid reasons why this was explicitly done (costs of disambiguating comes to mind). Or this could simply be an oversight of the implementor. Again only they know the answer.

If this does feel like a bug to you then please do file a bug on connect. This is the best way to get your opinion heard and very likely the team has never received any feedback on this behavior (could not find any in my searches).

like image 20
JaredPar Avatar answered Nov 06 '22 21:11

JaredPar