[SOLVED] Global variables RAM allocation

Hi. Ultra newbie here. This program does work, despite exceeding the available RAM (2K) on my Nano:

int b[10000];

void setup() {
    Serial.begin( 115200 );
    while( !Serial );
    for(int i = 0; i < 10000; ++i)
      b[i] = i;
    for(int i = 0; i < 10000; ++i)
void loop() {

The compiler states:

  • 1658 byte (5%) of program space
  • 188 byte (9%) of dynamic memory

How is it possible? I tried to search this forum and the internet, without any success.

That is not a particularly severe test. Try:
Serial.println( b[ i ] ) ;

A compiler optimisation has exclude the worst of it.

1 Like

The optimizer notices you don’t use the b array in any meaningful way and thus gets rid of that code altogether

1 Like

OK, thank you, now begins to became clearer. So the rule is: you can declare whatever you want; you can even write to it; but the compiler warns/stops you only when you try to read from it. Am I correct?

Could you please suggest a link to documentation that explains deeply how the optimizer works? Thank you

If you are indeed an "Ultra newbie", a deeply explanation of the optimizer is going to be a giant waste of you time.

Just sayin'.


I'm an ultra newbie, but in Arduino stuff. Anyway thank you for wasting your time answering with no additional useful info.

1 Like

It's all here and should be more or less self explanatory: Optimize Options (Using the GNU Compiler Collection (GCC))
Arduino for boards like the Uno etc. is currently at C++11

1 Like

Thank you!

And compiler uses -O3

So now you just have to look at what all those flags do (if that explains @alto777’s remark)

-falign-functions -falign-jumps
-falign-labels -falign-loops
-fcse-follow-jumps -fcse-skip-blocks
-fdevirtualize -fdevirtualize-speculatively
-fgcse -fgcse-lm
-fipa-bit-cp -fipa-cp -fipa-icf
-fipa-ra -fipa-sra -fipa-vrp
-freorder-blocks-and-partition -freorder-functions
-fschedule-insns -fschedule-insns2
-fsched-interblock -fsched-spec
-ftree-switch-conversion -ftree-tail-merge

Don't be rude. @alto777's comment was entirely legitimate.