Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 633
GilLevi Avatar asked Jul 27 '13 17:07

GilLevi


People also ask

How do I enable a watch window in Visual Studio?

Select the variable a in the code. Select Debug > QuickWatch, press Shift+F9, or right-click and select QuickWatch.

How do I add a code to my Visual Studio watch?

Right click on the variable in code, and select "Add Watch" Right click on the variable in the "Locals" windows, and select "Add Watch" Type the variable name into a new row in the Watch pane itself.


1 Answers

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
like image 91
Muhammad Annaqeeb Avatar answered Oct 08 '22 18:10

Muhammad Annaqeeb