GilLevi
GilLevi

Reputation: 2137

add watch shows undefined identifier visual studio 2012, cpp

I encounter the strangest behavior in VS 2012 (I'm writing in cpp).

I click "add watch" on a variable and it says "identifier is undefined".

Here's an example of the code:

for (int j=0;j<32;j++)
        {
            unsigned char curValue=desc1.at<unsigned char>(0,j);
            printf("%s\n",curValue);    
        }

I had to use printf to show the value of curValue. lol.

Has anyone encountered such behavior?

Edit: more strange this occur. When debugging the following code:

    int b1[8];

    for (int k=0;k<7;k++)
        b1[k]=0;

    char q=curValue; 
    int t=0;
    while (q!=0){
        b1[t++]=q%2;
        q=q/2;
    }

The debugger just skips the loop with b1[k]=0;

Please note curValue is undefined even inside the loop.

Thanks!

Upvotes: 17

Views: 18514

Answers (7)

HAL9000
HAL9000

Reputation: 3751

Restart Visual Studio. Worked for me.

Upvotes: 0

Mohamed El-Nakeep
Mohamed El-Nakeep

Reputation: 6718

As Joachim said: curValue is defined inside the loop. If watch window in visual studio see it as undefined value then you should turn off Compiler optimization.

Compiler optimization default is /O2 optimize for speed. To turn it off :

  • Go to project, right click and choose properties
  • Configuration Properties->C/C++->Optimization
  • select optimization , and change it from Maximize Speed (/O2) to Disabled (/Od) enter image description here

Upvotes: 26

Adrian McCarthy
Adrian McCarthy

Reputation: 47962

Without the printf, the first loop has no side effects and is thus likely optimized away in an optimized build. In the second example, the loop that initializes the small array to 0 is probably replaced with an initialized data section.

You should probably try to debug with an unoptimized build.

Also note that the Visual Studio debugger has pretty good visualizers for the standard containers. So if the whole point of the first loop was just to peek at the contents of desc1, you can probably just examine it directly in the debugger.

Upvotes: 2

GilLevi
GilLevi

Reputation: 2137

I had optimizations turned on. That messed up my debugging.

Upvotes: 4

Michael
Michael

Reputation: 16142

Variable curValue is only valid inside the loop. If you will try to add it to the "watch" when you aren't in the loop, then your variable will be not defined.

Note: Better you should print this:

printf("%c\n",curValue);

Instead of this:

printf("%s\n",curValue)

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122383

Because curValue gets out of scope outside the for loop.

Also note that you should use %c in printf to print char. %s is used for C-style strings.

printf("%c\n",curValue);

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409196

Remember that the variable curValue is only valid inside the loop, if you try to add it to the watch when you're not in the loop then the variable is not defined.

Upvotes: 1

Related Questions