Q - About Compile Time V Run Time and arrays size.

I have a program that when complied takes 30480Bytes. I’m using a Due board.

If I change the value of BAM_RESOLUTION I would expect that the amount of space that is required would change, but it appears not to.

EG when BAM_RESOLUTION = 4, red[BAM_RESOLUTION][Size_Cube / 8]; should require 4 x 8 x 8 x 12 / 8 = 348 bytes. There are 6 of these arrays so 348 x 6 = 2088 Bytes total.

When I make BAM_RESOLUTION = 8, then the total space required for the above arrays should be twice the amount above, 4176 Bytes. However at compile time the space required is still 30480 bytes

My Q is, at compile time, does the amount of space required for the program not take in to consideration the space that my arrays require? And if this is true, what happens at RunTime if the space required is greater than the space available?

``````#define BAM_RESOLUTION 4    // EG 4 bit colour = 15 variation of R, G & B (4096 colours)
const  byte Size_Y = 12;//Number of Layers Y axis (levels/Layers)
const  byte Size_X = 8; //Number of LEDs X axis (Left to right across front)
const  byte Size_Z = 8; //Number of LEDs Z axis (front to back)
const  int Size_Layer = Size_X * Size_Z; // Number of LEDs per Layer
const  int Size_Cube = Size_Layer * Size_Y; //Number of LEDs whole Cube.
const  int Size_CW = ((1 << BAM_RESOLUTION) - 1) * 6; //Number of steps in colour wheel. Why x 6, cos any more will give repeat colours.

//************* Cube Array ****************
byte red[BAM_RESOLUTION][Size_Cube / 8];
byte green[BAM_RESOLUTION][Size_Cube / 8];
byte blue[BAM_RESOLUTION][Size_Cube / 8];
byte Buf_red[BAM_RESOLUTION][Size_Cube / 8];
byte Buf_green[BAM_RESOLUTION][Size_Cube / 8];
byte Buf_blue[BAM_RESOLUTION][Size_Cube / 8];

// ***********  Colour Variables ***************
int Colour[Size_CW][3]; //Used to store colour for the cube in R, G and B = [3]
byte myRed, myGreen, myBlue; //Used to call colour from GetColour routine
int myColour = 0;// General purpose variable.
``````

Program space may be allocated in blocks larger than 4176-2088.

Just tried a couple of things.

This code requires 22076 bytes

``````void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}
``````

and this code requires 22364 bytes, which is odd to me…
As I have an ARM processor, an INT is 4 bytes x 10 x 1000 = 40 000 bytes! This code works as expected.

``````int TestArray[10][1000];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}

void loop() {
// put your main code here, to run repeatedly:
for (int TEST = 0; TEST < 10; TEST++) {
for (int Number = 0; Number < 1000; Number++) {
TestArray[TEST][Number] = Number;
Serial.println(TestArray[TEST][Number]);
}
}
}
``````

Now if I change the code to this, where TestArray now is 4 bytes x 100 x 1000 = 400 000 bytes.

``````int TestArray[100][1000];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}

void loop() {
// put your main code here, to run repeatedly:
for (int TEST = 0; TEST < 100; TEST++) {
for (int Number = 0; Number < 1000; Number++) {
TestArray[TEST][Number] = Number;
Serial.println(TestArray[TEST][Number]);
}
}
}
``````

I get an error MSG

Arduino: 1.8.3 (Windows 10), Board: "Arduino Due (Programming Port)"
c:/users/chris/appdata/local/arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/…/lib/gcc/arm-none-eabi/4.8.3/…/…/…/…/arm-none-eabi/bin/ld.exe: address 0x200d2d7c of C:\Users\Chris\AppData\Local\Temp\arduino_build_211424/sketch_jul03b.ino.elf section `.bss' is not within region `ram’
c:/users/chris/appdata/local/arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/…/lib/gcc/arm-none-eabi/4.8.3/…/…/…/…/arm-none-eabi/bin/ld.exe: address 0x200d2d7c of C:\Users\Chris\AppData\Local\Temp\arduino_build_211424/sketch_jul03b.ino.elf section `.bss' is not within region `ram’
c:/users/chris/appdata/local/arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/…/lib/gcc/arm-none-eabi/4.8.3/…/…/…/…/arm-none-eabi/bin/ld.exe: address 0x200d2d7c of C:\Users\Chris\AppData\Local\Temp\arduino_build_211424/sketch_jul03b.ino.elf section `.bss' is not within region `ram’
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Due (Programming Port).

It would appear that the space required is not shown after compiling but if you exceed the space available you get the above error…

Anyone???

One more quick test.
This code compiles at 22324 bytes. Now when I upload this and run it. mySize is, you guessed it, 40000 bytes. one int = 4 bytes x 10 x 1000 = 40000 bytes.

``````int TestArray[10][1000];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);

}

void loop() {
// put your main code here, to run repeatedly:
long mySize = sizeof(TestArray);
Serial.println(mySize);
}
``````

So I guess the above code really requires 62324 bytes of space to run and compiling does not take in to consideration the space required for the array itself.