static variable not initializing when following millis()

bicycleguy

Well-known member
Using Arduino IDE 2.3.3 on MacOS 14.6.1 Sonoma, Teensy Loader 1.59

The code below:
Code:
int gdefaultState=7;

void setup() {
  Serial.begin(115200);
  while(!Serial);
    ;
  
  Serial.printf("in setup gdefaultState=%d \n", gdefaultState);
}

void loop() {
  static boolean finished=false;
  static  uint32_t lastime=millis();
  static int state=gdefaultState;  //why can't this line follow the millis() line?
 
  if(!finished){
    Serial.printf("state=%d, gdefaultState=%d \n", state, gdefaultState);
    finished=true;
  }
}

produces:
Code:
in setup gdefaultState=7
state=0, gdefaultState=7
If the commented line is moved before the line above it I get the proper results:
Code:
in setup gdefaultState=7
state=7, gdefaultState=7
what up ?
 
Last edited:
Works correctly for me as shown in your post. I'm using T4.1, IDE 1.8.19, TD 1.59, Windows 7 Pro.

in setup gdefaultState=7
state=7, gdefaultState=7
 
I tried it on Teensy 3.2 using Arduino IDE 2.3.3 and latest 1.60 beta 3, running on Linux x86-64.

Also prints 7.

1728706458926.png
 
What could go wrong ? :)

1.60-beta3 is probably a good place to be. It has a number of fixes for issues discovered in prior versions (those mostly came in 1.60-beta1), but so far most new work has focused on updating the PC side software (MacOS version in particular). Next few betas will probably focus on new features on the Teensy side.
 
This smells like the __cxa_guard bug that we fixed back in April, the static variables inside loop() would trigger it when they first get initialized. So 1.60-x would be safe to use (1.59-x would show the bug).
 
Last edited:
Back
Top