add watch shows undefined identifier visual studio 2012, cpp

c++, visual-studio, watch

Solution

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)

Problem

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!

Original source