3 Replies Latest reply on Jul 2, 2015 1:35 PM by ScottA_91

    Optimized Out variables


       I have been having trouble with this optimizing out of variables in several projects and it seems I do not understand when this happens.


      In the last case I had a variable defined within scope of a procedure (method) that retrieved info from a serial port buffer and used the value to store a record in SPI flash IC. I thought the variable would only be optimized out if I did not use it.


      This is what I am doing:


      uint16 level;


      level = buf[3]*256 + buf[4];


      StoreRecord(.....,level);  //I though variable would be retained because I am using it in another function




      In this case I had to set up "level" as volatile. 


      I thought it would only be optimized out if not used, like this...


      uint8 level;


      Level = buf[3]*256 + buf[4];


      //then do nothing else with it