Pages: [1]   Go Down
Author Topic: Bug: Ethernet Library Sample Code WebClientRepeating  (Read 1686 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 2
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I originally posted this in http://arduino.cc/forum/index.php/topic,125510.0.html but since it appears to be a bug I will repost it here as well.

In the WebClientRepeating sample code for the Ethernet library there is a line for determining the posting interval.   The line is

Code:
const unsigned long postingInterval = 60*1000;  // delay between updates, in milliseconds

The problem is the actual value of the postingInterval is wrong when I print it out (it is 4294961760). It seems to be a casting/overflow problem with the compiler/preprocessor. It looks like it is treating the 60 and 1000 as signed integers.

I broke this out into very short sample code to demonstrate:
Code:
const unsigned long postingInterval = 60*1000;
const unsigned long postingIntervalB = (long)60*1000;

void setup() {
  Serial.begin(9600);
  Serial.println(postingInterval);
  Serial.println(postingIntervalB); 
}

void loop() {}

When I run this I get back the following values:
Code:
4294961760
60000


Other Ethernet samples use 10 seconds (10*1000) and do not appear to have this problem.  I assume it is because it is withing the range of an signed integer and that is how the compiler/preprocessor is treating the values. 
Logged

Forum Administrator
Offline Offline
God Member
*****
Karma: 52
Posts: 646
I find plain exciting
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've forwarded your message to the testers to get a diagnose smiley

thanks
Logged

Global Moderator
Milano, Italy
Offline Offline
Edison Member
*****
Karma: 23
Posts: 1180
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

the problem with this line:
Code:
const unsigned long postingInterval = 60*1000;  // delay between updates, in millisecond

is that the two numbers (60 and 1000) are by default defined as int constants. For AVRs ints have the size of 16-bit, so the multiplication between two ints with a result that will exceed the maximum dimension for the type, generate an overflow.

The solution should be the variable cast that you propose or assigning the constant with a different type, like:

Code:
const unsigned long postingInterval = 60L*1000;  // Long typ e constant

or

const unsigned long postingInterval = 60000;  //assigning directly


This example will be fixed in the next IDE release.
Thanks for reporting!

Logged

F

Pages: [1]   Go Up
Jump to: