1

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?

bolov
  • 72,283
  • 15
  • 145
  • 224
  • marking the array my_array[] as volatile creates a compiler warning and is ignored. Other solutions besides using the array in a dummy line? – Panos Kontogiannis Dec 30 '20 at 03:44
  • By turning the compiler optimization level to None (-O0) solves part of the problem but is not an answer to my problem. – Panos Kontogiannis Jan 01 '21 at 08:47
  • 3
    Can you create a [mcve], and state the compiler/version you are using? Usually this sort of thing is due to undefined behavior elsewhere in the code. – Nate Eldredge Jan 02 '21 at 02:13
  • What do you mean with **reused**? – ssbssa Jan 02 '21 at 02:14
  • I tried to minimize it, but I can't reproduce the problem. Now I suspect that I have a timing problem in a time-critical part that the small delay introduced by the call to the UART function is hiding. Searching elsewhere, thank U. – Panos Kontogiannis Jan 02 '21 at 16:45

1 Answers1

0

After experimenting with "volatile" keyword, I gave up and moved the assignment inside the my_function(), then turned the optimization level to o0, only for the particular function, adding __attribute__((optimize("O0"))) to the function declaration, as suggested in this link. It works.

  • 1
    Your program most definitely has Undefined Behavior. It's the only explanation for the weird behavior (other than the highly unlikely compiler bug). `-O0` might seem to fix the problem but it only hides it ... for now. It might come to bite you a few changes down the line. A seemingly unrelated change in another part of the program will trigger strange behavior again. Or just a compile update or another set of compiler flags. I strongly encourage you to find and fix the bug in the code instead. – bolov Jan 02 '21 at 02:15
  • likely culprits for the UB (without seeing the rest of the code): strict aliasing violation or out of bounds access. https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule , https://stackoverflow.com/questions/98340/what-are-the-common-undefined-unspecified-behavior-for-c-that-you-run-into – bolov Jan 02 '21 at 02:22
  • Most probably an SPI timing problem, so in a way you are right. Thank U – Panos Kontogiannis Jan 02 '21 at 16:48