user21494: I burned the code written below, I expect that as we declare the local variable in the loop, freeMemory() function should return decreased value. But it remains constant..! please explain why..?  The output is attached :
#include <MemoryFree.h>
void setup() 
{
  // put your setup code here, to run once:

  Serial.begin(9600); Serial.println(freeMemory(), DEC);  // print how much RAM is available.
  Serial.println("Lets Start");
}
void jack();
void loop() 
{
    char c=0;
    c++;
    Serial.println("");
    Serial.println("Rajat0");
    Serial.println(freeMemory(), DEC);
    Serial.println("Rajat1");
    int x=0;
    x++;
    Serial.println("Rajat2");
    Serial.println(freeMemory(), DEC);
    Serial.println("Rajat3");
    jack();
    Serial.println("");
    delay(1000);
}
void jack()
{
    Serial.println("jack0");   
    int x=0;
    x++;
    Serial.println(freeMemory(), DEC);
    Serial.println("jack1");      
}



user4143: You are assuming that, just because you created a variable, it will be stored in memory. That is often not the case.
The compiler is reasonably intelligent. And there are lots of CPU registers in the AVR core. If there is a free register available for a temporary local variable like that, why would it waste time and effort putting it into memory when it can just keep it in a register?
Also, since you don't really do anything with the variable, chances are the compiler is just throwing it away.
Marking a variable volatile will remove both those problems, and you may see different results.

21494: <p>I burned the code written below, I expect that as we declare the local variable in the loop, freeMemory() function should return decreased value. But it remains constant..! please explain why..?  The output is attached :</p>

<pre><code>#include &lt;MemoryFree.h&gt;
void setup() 
{
  // put your setup code here, to run once:

  Serial.begin(9600); Serial.println(freeMemory(), DEC);  // print how much RAM is available.
  Serial.println("Lets Start");
}
void jack();
void loop() 
{
    char c=0;
    c++;
    Serial.println("");
    Serial.println("Rajat0");
    Serial.println(freeMemory(), DEC);
    Serial.println("Rajat1");
    int x=0;
    x++;
    Serial.println("Rajat2");
    Serial.println(freeMemory(), DEC);
    Serial.println("Rajat3");
    jack();
    Serial.println("");
    delay(1000);
}
void jack()
{
    Serial.println("jack0");   
    int x=0;
    x++;
    Serial.println(freeMemory(), DEC);
    Serial.println("jack1");      
}
</code></pre>

<p><a href="https://i.stack.imgur.com/z4wby.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z4wby.png" alt="enter image description here"></a></p>
 4143: <p>You are assuming that, just because you created a variable, it will be stored in memory. That is often not the case.</p>

<p>The compiler is reasonably intelligent. And there are lots of CPU registers in the AVR core. If there is a free register available for a temporary local variable like that, why would it waste time and effort putting it into memory when it can just keep it in a register?</p>

<p>Also, since you don't really <em>do</em> anything with the variable, chances are the compiler is just throwing it away.</p>

<p>Marking a variable <code>volatile</code> will remove both those problems, and you may see different results.</p>
