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

StackOverflow https://stackoverflow.com/questions/4649363

Question

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")

Was it helpful?

Solution

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).

OTHER TIPS

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

What you're running into is the debugger's 'memory operator':

In native C++, debugger expressions do support the following additional operators:

  • The context operator ({ }) to specify the context of a symbol. For more information, see Context Operator (C/C++ Language Expressions).

  • Memory operators (BY, WO, and DW) to access memory. Memory operators have the lowest precedence of any operator. The memory operators are useful mainly in debugging assembly-language code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top