title: Arduino IDE - global variables storage in RAM or flash memory
body: <p>Conventional wisdom has it that global and static data is stored in the bottom of RAM along with other stuff. Somewhere above that is the heap, then free memory and at the top of RAM, is the stack. See the code below before reading on.</p>
  <p>When I compile this with the Arduino IDE (1.8.10) on a MEGA2560 I get the following statistics:</p>
  <p>Sketch uses 2750 bytes (1%) of program storage space. Maximum is 253952 bytes.
  Global variables use 198 bytes (2%) of dynamic memory, leaving 7994 bytes for local variables. Maximum is 8192 bytes.</p>
  <p>If I change ARRAY_SIZE from 1 to 7001, I get exactly the same numbers. I expected that dynamic memory should increase by 7000. When I do the same comparison with AtmelStudio V7, dynamic memory does indeed increase by 7000.</p>
  <p>One more piece of information along these lines. If I do a malloc of 7000 which is pretty close to free memory, one would expect that the malloc should be successful when ARRAY_SIZE equals one and would fail when the ARRAY_SIZE equals 7001. I was dismayed to find that the malloc was successful with both the small and the large array sizes. With AtmelStudio this does not happen.</p>
  <p>I suspect respective compiler/linker options somewhere could explain the difference (AtmelStudio - project properties and Arduino IDE - platform.txt perhaps?).</p>
  <p>I also suspect that the Arduino IDE dynamically allocates global variables in FlashMemory.</p>
  <p>I am not a newbie, but I am not a guru - comments anyone?</p>
  <p>Thanks
  Michèle</p>
  <pre><code>#define ARRAY_SIZE 7001
  uint8_t globalArray[ARRAY_SIZE]{1};
  void setup() {
   Serial.begin(115200);
   for (int i = 0; i &lt; ARRAY_SIZE; i++) globalArray[i] = 1;
    Serial.print(F(&quot;Just initialized globalArray, size = &quot;));Serial.println(ARRAY_SIZE);
    uint8_t* testPointer = (uint8_t*) malloc(7000);
    Serial.print(F(&quot;Allocation of 7000 bytes &quot;));
    if ( testPointer != (uint8_t*) 0) {
    Serial.print(F(&quot;SUCCESSFUL&quot;));
     } else {
     Serial.print(F(&quot;NOT SUCCESSFUL&quot;));
     }
  } // setup
  void loop() {} // loop
  </code></pre>