Bug in IDE 1.8.9: issues in the execution of switch/case/default statements

Hi,

I've just updated my IDE to version 1.8.9 and I believe I've found a bug in the compilation of switch/case/default statements.

A variable is both declared and initialised within a case statement (see test code attached)

  • the case statements up to the one in question (included), work fine;
  • the case/default statements that follow the one in question, they DO NOT WORK.

Even though the compiler doesn't complain, some of the libraries I've developed in the past started to behave oddly because of that :frowning:

There is a workaround solution, but...

#include <Arduino.h>

void setup() {
  Serial.begin(115200);
  while (!Serial);
  switch (2) {
    case 0:
      break;
    case 1:
      // case 2 doesn't work
      byte c = 1;
      /*
      // case 2 works
      byte c;
      c = 1; 
       */
      break;
    case 2:
      Serial.println(F("Ciao!"));
      break;
  }
  Serial.println(F("That's all folks!"));
}

void loop() {
}

Testing environment: IDE 1.8.9 on MAC + Arduino Mega 2560

Ciao,
Iacopo.

Why did you think it is a big?
In C++ it’s illegal to declare variables (without the additional {} stack frame scoping) inside a switch/case statement:

I definitely think that this is a problem. I spend a lot of hours tracking this down in my application. If there was compiler warning - that could be enough ( I got no notice even with all warnings enabled).

To the exapmple that ibaronci gave:
The behaviour is very odd, the second case (that doesn't contain declared variable) is never executed... that is something different than the thing described in stackOverflow topic.

(Arduino Uno + IDE 1.8.13)

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.