This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

variable suddenly reset to 0

Hi ti all,
i am developing a device with keil MDK arm and i encountred with an strange behavior, look at below code:

uint16_t  var = 1;
uint16_t* ptr = 0;

for(var = 1; var < 100; var++)
{
   do_some_thing();
   //this function always reset the var to 0
}

in above code after calling the do_some_thing(), var is resetting to 0, but in below code this did not happen:

uint16_t  var = 1;
uint16_t* ptr = 0;
ptr = &var;

for(var = 1; var < 100; var++)
{
   do_some_thing();
   //this function always reset the var to 0
}

i can not understand reason of this behavior, i think that it may be a problem with keil code optimization but anyway, can any one explain me that why this behavior occur ??

thanks

  • Most likely an issue with the stack, heap and statics colliding. Make sure your stack allocation is sufficient to accommodate all your local/auto variables for however deep your call tree is in these other functions.

  • How are you determining that it is reset to 0?

    Does the code actually not execute properly or did you make an assumption that it was not actually running properly?

    My guess is that do_something() is actually called 99 times in both cases and functioning properly. If the variable was being reset to 0 then it would never finish the loop.