VS Arduino
Visual Micro >> General Board >> Odd behaviour of conditional operator "?"
https://www.visualmicro.com/forums/YaBB.pl?num=1634743992

Message started by Khtos on Oct 20th, 2021 at 3:33pm

Title: Odd behaviour of conditional operator "?"
Post by Khtos on Oct 20th, 2021 at 3:33pm
Hello, community!
I have noticed a strange behavior of the conditional operator “?” while working with bool variables.
First of all, the bool variable was initialized incorrectly. I restored it from an uninitialized EEPROM, so the variable contained 0xff instead of 0 or 1.
The attached program just demonstrates the issue.
The program was compiled for teensy 4.0 and teensy 4.1 and show the same results for both boards.
Here is the list of the program:

Code (c++):

bool     Bool_var;
uint8_t  temp = 0x7f;
uint8_t  Key = 0;
uint8_t  OldKey = 0;

void setup() {
     Serial.begin(115200);
     while (Serial) { ; }
   
     *(uint8_t*)&Bool_var = 0xff;      // initially here was restoring a variables from EEPROM, which wasn't initialized!
     
     pinMode(0, INPUT_PULLUP);      // hardware button connected to pin 0
}

// the loop function runs over and over again until power down or reset
void loop() {

     Key = digitalReadFast(0);                        // reading the hardware button and try to invert the bool variable on each pressing
     if ((Key == 0) && (OldKey == 1)) {                  //
           //Bool_var = Bool_var ? false : true;            // initially i saw issue in this expression, but during the studying
                                                           // of the problem, I changed the code to one that is not commented out.
           temp = Bool_var ? 0 : 1;                  // if I change this expression, to use other values, not just 0 and 1,
                                                                // everything starts to work perfectly.
           //temp = Bool_var ? 0 : 5;                      // For example, this expression works fine.
           Bool_var = (bool)temp;

           Serial.printf("temp=%i; Bool_Var=%i; ", temp, Bool_var);    // output results to terminal
           if (Bool_var)  Serial.printf("Bool_Var=true;\n");
           else           Serial.printf("Bool_Var=false;\n");
     }

     OldKey = Key;
     delay(10);
}


And here is a result from terminal:


[code]Opening port Port open
temp=254; Bool_Var=254; Bool_Var=true;
temp=255; Bool_Var=255; Bool_Var=true;
temp=254; Bool_Var=254; Bool_Var=true;
temp=255; Bool_Var=255; Bool_Var=true;
temp=254; Bool_Var=254; Bool_Var=true;
temp=255; Bool_Var=255; Bool_Var=true;[/code]
As you can see, Bool_Var doesn’t change and keep the “true” value.  Moreover, incorrect values are assigned to the “temp” variable, which, in principle, should not happen!

As a solution for this problem I’m going not to use the “?” function anymore, as the old “if… else… ” works fine, but I wonder if anybody met this behavior? Is it really a glitch or I must connect some library in order to "?" works properly?


Title: Re: Odd behaviour of conditional operator "?"
Post by Simon@Visual Micro on Oct 20th, 2021 at 4:55pm
Thanks for the report.

I believe the bool variable in Arduino is actually uint8_t beneath, hence the ability to store a value > 1 into it.  This behaves the same in the Arduino IDE as well.

One option is to use the below line of code instead to ensure the conditional operator evaluates the expression to true or false in all scenarios.
[code]temp = ( Bool_var >= 1 ) ? 0 : 1;[/code]

For more detailed support with coding specific help, the Arduino forums, and StackOverflow offer a wider forum for C++ and Arduino platform support.

Title: Re: Odd behaviour of conditional operator "?"
Post by Khtos on Oct 21st, 2021 at 12:49pm
Thank you for the answer!
I read a couple of your answers on this forum, and now I understand, that the question is outside the sphere of your responsibility, you just use third-party compilers. Answer, please, one more question. I compiled my code for teensy4. In order to work with this board, I installed Arduino IDE and then install Teensyduino over it. So, I’d like to know, which team should I send the question about the behavior of "?": Arduino or Teensyduino?

What about your construction

[code]temp = ( Bool_var >= 1 ) ? 0 : 1; [/code]
of course, it works. I know, there are many ways to get around this problem, but my perfectionism and C ++ manual says that everything should work as it is!
About your suggestion of the bool variable in Arduino is the same that uint8_t, I think you are wrong, because if I do nothing, but just change the type of Bool_var to uint_8t, the program starts to work perfectly! Thus, bool is processed in a different way than uint_8t. :)


Title: Re: Odd behaviour of conditional operator "?"
Post by Tim@Visual Micro on Oct 21st, 2021 at 8:02pm
For teensy4 you are using the compiler from the manufacturer of Teensy at https://pjrc.com

They in turn often rely on tools provided by the manufacturer of the microcontroller that is used by the board. Therefore pjrc might have to point you to a wider discussion.


VS Arduino » Powered by YaBB 2.6.12!
YaBB Forum Software © 2000-2024. All Rights Reserved.