Serial.parseInt() problem ... int vs long return type

I ran into some Serial.parseInt() problems on 'arduino-1.0'. Here is a summary:

  • I think the documented return type (int) is wrong. I looked at Stream.parseInt() and found it returns a long. But the problem is more serious than documentation issues...
  • When I assign the result to an int per the documentation I get overflow. Basically, it seems to be overflowing as if parseInt() is returning a short.
  • When I assign the result to a long per the return type implemented in Stream.parseInt() all is well and I get no overflow.

with some simple code...
int val= Serial.parseInt();
Serial.println(val);
and serial input...
0 1 100 10000 32767 32768 65535 65536 1000000
I get the following surprising response values...
0 1 100 10000 32767 -32768 -1 0 16960

with revised code...
long val= Serial.parseInt();
Serial.println(val);
I get the following expected response values...
0 1 100 10000 32767 32768 65535 65536 1000000

I think the documentation needs to be updated as returning long. I am guessing the overflow has nothing to do with Arduino libraries but instead with some implicit type conversions the compiler adds. Can anyone officially explain this overflow?

Thanks, Core

But the problem is more serious than documentation issues...
When I assign the result to an int per the documentation I get overflow. Basically, it seems to be overflowing as if parseInt() is returning a short.

If you know the documentation is wrong, why are you assigning the return value to an int?

When I assign the result to a long per the return type implemented in Stream.parseInt() all is well and I get no overflow.

Well, duh.

Can anyone officially explain this overflow?

It's what happens when you try to stuff a large value in a small memory location.

PaulS:

But the problem is more serious than documentation issues...
When I assign the result to an int per the documentation I get overflow. Basically, it seems to be overflowing as if parseInt() is returning a short.

If you know the documentation is wrong, why are you assigning the return value to an int?

When I assign the result to a long per the return type implemented in Stream.parseInt() all is well and I get no overflow.

Well, duh.

Can anyone officially explain this overflow?

It's what happens when you try to stuff a large value in a small memory location.

Hey PaulS,

Wow, you were so focused on being rude you couldn't answer the question. Well Duh... you didn't explain how it overflows as a short value when being assigned to an int.

Anyway, it is important for other people to realize that you don't just get the value truncated to an 'int' if you use an int type, but you get a 'short'. Serious unexpected data loss. This is not obvious and it is important for others to know this too.

Core

Anyway, it is important for other people to realize that you don't just get the value truncated to an 'int' if you use an int type, but you get a 'short'.

The Arduino doesn't have a short type. It has 16 bit ints and 32 bit longs.

PaulS:

Anyway, it is important for other people to realize that you don't just get the value truncated to an 'int' if you use an int type, but you get a 'short'.

The Arduino doesn't have a short type. It has 16 bit ints and 32 bit longs.

Part of that was helpful. Thanks!

But "Well duh" are you completely correct? This short piece of code compiles for me and runs on the "Arduino".
short paulEatsShorts= 666;
Serial.println(paulEatsShorts);
Hmm. Better check your documentation to find how you lost your shorts.

I wasn't clear in my last post. I now understand that on the Arduino...
short and int are 16-bits
long is 32-bits
and someone still needs to update the Stream.parseInt() docs :wink: