[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)
      Serial.println(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'.

a7

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)

-fauto-inc-dec
-fbranch-count-reg
-fcombine-stack-adjustments
-fcompare-elim
-fcprop-registers
-fdce
-fdefer-pop
-fdelayed-branch
-fdse
-fforward-propagate
-fguess-branch-probability
-fif-conversion
-fif-conversion2
-finline-functions-called-once
-fipa-modref
-fipa-profile
-fipa-pure-const
-fipa-reference
-fipa-reference-addressable
-fmerge-constants
-fmove-loop-invariants
-fmove-loop-stores
-fomit-frame-pointer
-freorder-blocks
-fshrink-wrap
-fshrink-wrap-separate
-fsplit-wide-types
-fssa-backprop
-fssa-phiopt
-ftree-bit-ccp
-ftree-ccp
-ftree-ch
-ftree-coalesce-vars
-ftree-copy-prop
-ftree-dce
-ftree-dominator-opts
-ftree-dse
-ftree-forwprop
-ftree-fre
-ftree-phiprop
-ftree-pta
-ftree-scev-cprop
-ftree-sink
-ftree-slsr
-ftree-sra
-ftree-ter
-funit-at-a-time
-falign-functions -falign-jumps
-falign-labels -falign-loops
-fcaller-saves
-fcode-hoisting
-fcrossjumping
-fcse-follow-jumps -fcse-skip-blocks
-fdelete-null-pointer-checks
-fdevirtualize -fdevirtualize-speculatively
-fexpensive-optimizations
-ffinite-loops
-fgcse -fgcse-lm
-fhoist-adjacent-loads
-finline-functions
-finline-small-functions
-findirect-inlining
-fipa-bit-cp -fipa-cp -fipa-icf
-fipa-ra -fipa-sra -fipa-vrp
-fisolate-erroneous-paths-dereference
-flra-remat
-foptimize-sibling-calls
-foptimize-strlen
-fpartial-inlining
-fpeephole2
-freorder-blocks-algorithm=stc
-freorder-blocks-and-partition -freorder-functions
-frerun-cse-after-loop
-fschedule-insns -fschedule-insns2
-fsched-interblock -fsched-spec
-fstore-merging
-fstrict-aliasing
-fthread-jumps
-ftree-builtin-call-dce
-ftree-loop-vectorize
-ftree-pre
-ftree-slp-vectorize
-ftree-switch-conversion -ftree-tail-merge
-ftree-vrp
-fvect-cost-model=very-cheap
-fgcse-after-reload
-fipa-cp-clone
-floop-interchange
-floop-unroll-and-jam
-fpeel-loops
-fpredictive-commoning
-fsplit-loops
-fsplit-paths
-ftree-loop-distribution
-ftree-partial-pre
-funswitch-loops
-fvect-cost-model=dynamic
-fversion-loops-for-strides

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