2021年1月1日星期五

gcc optimization ignores program line

Programming the Attiny1634, I try to reuse ram space. Doing so I reused my_array[0] first element to send a u8 value to my_function requiring a pointer to uint8_t, but whatever I did, the function got the wrong value. Strangely when I printed the value before calling my_function everything works as intended. My suspicion is that setting the array value is ignored by the compiler when I do not use it and using its pointer does not count as using it.

uint8_t my_array[2];  ...  my_array[0] = some_value;    // Serial0_print_U8(my_array[0]);    // when I uncomment this line everything works    my_function(my_array);    // my_function gets a wrong val if I comment out the debug print  void my_function(const uint8_t* val){      Serial0_print_U8(val[0]);              // only prints some_value if I uncomment the print above  }    

Question what is the proper way to prevent the compiler from ignoring my line. Would volatile work?

https://stackoverflow.com/questions/65470720/gcc-optimization-ignores-program-line December 28, 2020 at 05:39AM

没有评论:

发表评论