unsigned long array assignment issue.

I’m having a problem with an array of unsigned ints that I can’t figure out. In the code below the arduino will not reach the establishConnect() function. Nothing gets sent over the serial:

unsigned long update_interval[3] = {0, 0, 0};

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  for (int i = 0; i < sizeof(update_interval); i++) {
    update_interval[i] = 0;
  }
  establishContact();
}

void loop()
{
  //
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println(".");   // send an initial string
    delay(300);
  }
}

However if I change the array to type of int, everything works, and periods begin getting sent over the serial:

int update_interval[3] = {0, 0, 0};

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  for (int i = 0; i < sizeof(update_interval); i++) {
    update_interval[i] = 0;
  }
  establishContact();
}

void loop()
{
  //
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println(".");   // send an initial string
    delay(300);
  }
}

Any ideas why this would be?

  for (int i = 0; i < sizeof(update_interval); i++) {

sizeof(update_interval) reports the number of bytes in the array, NOT the number of elements in the array.

sizeof(update_interval)/sizeof(update_interval[0]) is the number of elements in the array.

long or int, you are walking off the end of the array, stomping on data you don’t own. With long, you are stomping farther past the end of the array.

Because sizeof returns the number of bytes in the object, not the number of elements. You are just lucky it works with ints. You are clobbering memory.

you need sizeof(update_interval)/sizeof(update_interval[0]

But why? You know it is three elements.

EstablishConnect() or establishContact() ?

You are going beyond the end of your array, even with int. sizeof() does not do what you seem to think that it does. Look it up.

Man. I just figured this out, and came back to report on my stupid use of sizeof. You guys beat me to it. Thanks, though! Facepalm.

You can use a macro to get the element count of any data type:

#define ELEMENTS(x) (sizeof(x) / sizeof(x[0]))

unsigned long update_interval[3] = {0, 0, 0};

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  for (int i = 0; i < ELEMENTS(update_interval); i++) {
    update_interval[i] = 0UL;
  }
  establishContact();
}

void loop()
{
  //
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println(".");   // send an initial string
    delay(300);
  }
}

Also note the use of UL after the zero assignment in the loop. It helps document what you’re doing. Rather than use a loop, you could use:

memset(update_interval, 0, sizeof(update_interval));

to zero-out the array.

And note that sizeof() is determined at compile time, so there is no run-time penalty.