Go Down

Topic: 0 == 2048 ??? (Read 791 times) previous topic - next topic

Malvineous

Hi all,

I've got a bit of a weird problem building my library and I can't figure it out.  I have a function that the external code calls, and this function returns zero.  The external code has a while loop that checks this return value and breaks out of the loop when it's zero.

So you'd think the loop would never run, right?  Well the Arduino locks up in the loop...

Code: [Select]

uint8_t get_value() {
 return 0;
}
void external_function() {
 while (get_value()) {
   // do stuff
 }
}

Given that the code locks up only when the while loop is present I put a debugging command in to find out what get_value() is returning, and it came out as 2048!  That's why the loop runs forever.  How you get 2048 from a "return 0;" is beyond me though...any suggestions??

Maybe something to do with a uint8_t being cast to some other type?

Malvineous

Why is it that you can stuff around with a problem for ages and as soon as you post it on a forum you stumble across the solution?

Well I'm not sure *why* it works, but you have to explicitly cast the return value to the same type as in the function definition:

Code: [Select]

while ((uint8_t)get_value()) {

You'd think the compiler would do that automatically, but apparently not...

Maybe it's got something to do with the external code being 'extern C' and the get_value() function is not?

Coding Badly


Does a char return value make any difference...

Code: [Select]
char get_value() {

Malvineous

Unfortunately now I can't tell :-(  It works as a char, but it is also working now if I take out the uint8_t cast.  Perhaps that memory location is all zero now so it will work until it changes again...?

Coding Badly

Quote
Perhaps that memory location is all zero now so it will work until it changes again...?

If it's a code generation bug that could certainly be the case.  If it happens again, please report back.

Or, if you post the Sketch, someone may be willing to dig through the assembly to determine if there's still a problem.

Go Up