Welcome, Guest. Please Login or Register
Arduino IDE for Visual Studio
 
 
When Logging a Support Issue in the Forum, please ensure you have also:-
  • Enabled vMicro > Compiler > Show Build Properties and Verbose
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
  HomeSearch Tutorials & Wiki visualmicro.com LoginHelpRegister  
 
Page Index Toggle Pages: 1
Send Topic Print
Odd behaviour of conditional operator "?" (Read 133 times)
Khtos
Newbies
*
Offline



Posts: 2
Odd behaviour of conditional operator "?"
Oct 20th, 2021 at 4: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; 


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?

Back to top
 
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline



Posts: 1057
Re: Odd behaviour of conditional operator "?"
Reply #1 - Oct 20th, 2021 at 5: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; 



For more detailed support with coding specific help, the Arduino forums, and StackOverflow offer a wider forum for C++ and Arduino platform support.
Back to top
 
 
IP Logged
 
Khtos
Newbies
*
Offline



Posts: 2
Re: Odd behaviour of conditional operator "?"
Reply #2 - Oct 21st, 2021 at 1: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;  


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. Smiley

Back to top
 
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline



Posts: 11149
United Kingdom
Re: Odd behaviour of conditional operator "?"
Reply #3 - Oct 21st, 2021 at 9: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.

Back to top
 

Tim Leek
home
WWW  
IP Logged
 
Page Index Toggle Pages: 1
Send Topic Print