Wire.write(Data, DataAmount) – copies entire Data array and doubles RAM usage

Hello,

I have 315 member byte Array and want to send part of it via I2C, but Wire.write(Data, DataAmount) – copies entire Data array and doubles RAM usage.

I think Data is a pointer where to start, DataAmount is an amount of bytes from that pointer to send, so why it copies entire Array and increases RAM usage? :o

Please post a complete program that illustrates the problem

Compiled to Leonardo (32U4), nothing fancy:

#include <Wire.h>
byte Buffer[7][3][15];

void setup()
{
  Wire.begin();
}
void loop()
{
  Wire.write(Buffer[0][0], 15);
  delay(1000);
}

If Wire.write(Buffer[0][0], 15); commented out it uses 325 bytes, if not 640 bytes

When you comment out the Wire.write() statement, the compiler determines that the Buffer array is never used, so it removes it completely from the code. The difference between 640 bytes and 325 bytes is 315 bytes, which is the size of the array. If you change the dimensions of the array, you will see that the code size never changes without the Wire.write() statement, but will change a corresponding amount with that line of code included.

Hm, OK,

I was confused by 325bytes that are already in use, added "volatile" and removed Wire.write() and still it's 640 bytes, so wasn't Wire lib, it was compiler that messed with me :slight_smile:

Actually I spend almost two days troubleshooting error that appeared in part of the code that was done a while a go, and turns out compiler decided to not include some variables cos they wasn't "volatile", and it was pretty tricky to troubleshoot, wow, that was a lesson...

Thank You.