0

I am converting an signed int to send over Arduino Wire as multiple bytes so I did the following:

The code below is the debugging of my implementation, you can copy into the Arduino IDE to see printout.

What I cant understand is how itoa creates an array larger than the declared size which is not detectable by sizeof(). My declared array was size 2 but itoa used an array of size 6 to store its result!!

Serial.println("|----START DEBUG------|");
int Sensor1Data=-32760;
Serial.print("Sensor1Data: ");Serial.println(Sensor1Data);
byte Sensor1CharMsg[2];

Serial.println("|----STAGE 2------|");
Serial.print("Array Size b4 itoa: ");Serial.println(sizeof(Sensor1CharMsg));
itoa(Sensor1Data,(char*)Sensor1CharMsg,10);
Serial.print("Array Values up to 10 elements: ");Serial.write(Sensor1CharMsg,10); Serial.println("");
Serial.print("Array Size a4tr itoa: ");Serial.println(sizeof(Sensor1CharMsg));

Serial.println("||||||| ARRAY OUTPUT|||||||");
Serial.print("Sensor1CharMsg[0]): ");  Serial.println(Sensor1CharMsg[0]);
Serial.print("Sensor1CharMsg[1]): ");  Serial.println(Sensor1CharMsg[1]);
Serial.print("Sensor1CharMsg[2]): ");  Serial.println(Sensor1CharMsg[2]);
Serial.print("Sensor1CharMsg[3]): ");  Serial.println(Sensor1CharMsg[3]);
Serial.print("Sensor1CharMsg[4]): ");  Serial.println(Sensor1CharMsg[4]);
Serial.print("Sensor1CharMsg[5]): ");  Serial.println(Sensor1CharMsg[5]);
Serial.println("|||||||END ARRAY OUTPUT|||||||");

After transmission:

int Sensor2Data = atoi((char*)Sensor1CharMsg);
Serial.print("Sensor2Data: ");Serial.println(Sensor2Data);

Result

|----START DEBUG------|
Sensor1Data: -32760
|----STAGE 2------|
Array Size b4 itoa: 2
-32760
Array Size a4tr itoa: 2
||||||| ARRAY OUTPUT|||||||
Sensor1CharMsg[0]): 45
Sensor1CharMsg[1]): 51
Sensor1CharMsg[2]): 50
Sensor1CharMsg[3]): 55
Sensor1CharMsg[4]): 54
Sensor1CharMsg[5]): 48
|||||||END ARRAY OUTPUT|||||||

After Transmission

Sensor2Data: -32760
frazras
  • 5,928
  • 4
  • 30
  • 40

1 Answers1

4

You are overwriting memory, invoking undefined behavior.

Also, itoa() doesn't exactly create a "byte array", it creates a string. The name means "integer to ASCII". The documentation says:

The caller is responsible for providing sufficient storage [...]

Finally, a string's length is computed by strlen(), not by sizeof.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • `sizeof` works just fine on arrays (but not function array arguments, which are really pointers in disguise) – Ben Voigt Apr 02 '13 at 13:22
  • But for strings you want to use `strlen`. Because `strlen` works always (for zero terminated strings) and `sizeof` only in very few cases. The same is for any other array, you want to explicitly store the size and use that and not rely on `sizeof`. – rioki Apr 02 '13 at 13:39
  • @BenVoigt Huh? I was talking about strings, not arrays. – unwind Apr 02 '13 at 14:11
  • It's not that `sizeof` doesn't work, it's that it measures something different -- the amount of storage. And in this question, it seems like the amount of storage IS what he wanted to know. – Ben Voigt Apr 02 '13 at 14:37