5 Replies Latest reply on Aug 28, 2015 6:03 PM by DaHu_285096

    Variables being optimized out


      Maybe the weather is a bit colder today and this explains it?


      I am experiencing the compiler optimizing out most of my variables. I have made almost all of them volatile and still cannot prevent it happening. In the attached the array "Sensor[sensorLocation]" does not provide watch elements for the array and when I hover over "sensorLocation" the dialog announces it is "optimized out".


      I have a variable called "NumSensors" in the header file of a header file. Several other modules access the variable (which is also made volatile).


      The first module initializes the sensors and sets NumSensors ok. Then the first module calls another module (which does not use NumSensors at all), then when returned back to first module, NumSensors keeps setting back to 0. I had to move the declaration for NumSensors into the first module to prevent it loosing its value.


      What could be causing this behavior? I never experienced this in Creatore 3.1, then started to get some of these issues with Creator 3.2 and now it appears much worse in 3.2 SP2. Is there some compiler directive or something 3.2 , SP2 is switching on that affects the variable values being held in memory?

        • 1. Re: Variables being optimized out

          When variables are put into registers for optimization you will not see a correct value in the debugger.


          I would suggest to


          set compiler optimization to "None" (Remember you can set build options on a file base!)


          assign Sensor[sensorlocation] to a variable and check that in debugger





          • 2. Re: Variables being optimized out

            Thanks Bob,




            I did notice an issue with the way I declared the structs in the header files.


            I thought I could declare the array variable in the header file without specifying the size like this "SensorType Sensor[];" and then specify size of array in the C file. However, it seems I when I do this I cannot view the array in the debugger, but when I explicitly set size in header file I can see the array elements in debugger.


            typedef struct{
              uint8   fitted;
              uint16  detectedTagID;
              uint8   tripped;
              uint8   type;
              uint8   tripCount;
              uint8   tripLevel;
              uint8   location;
              uint8   lastRssi;
              uint8   noResponseTimeout;
              uint8   noResponseCount;
              uint8   noResponseTripped;


            extern volatile SensorType Sensor[MAXSENSORS];

            • 3. Re: Variables being optimized out

              The purpose of C was not to make life easier, but more adventurous. ;-)





              • 4. Re: Variables being optimized out

                And don't we all love a good adventure :) 


                BTW: I am not getting email notifications for post replies since the Cypress web update, Is there an option hidden away somewhere I need to select?



                • 5. Re: Variables being optimized out

                  Sorry, I was being lazy, I found the setting in my Profile -> Forums

                  1 of 1 people found this helpful