0

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");      
}

enter image description here

  • Given your recent track of similar questions that turn around how the compiler manages memory, I very strongly recommend you start to learn the basics of AVR assembly, and then read the assembly generated by the compiler. I promise this will enlightening. – Edgar Bonet Aug 4 '18 at 15:50
  • Thanks @Edgar for your kind suggestion..! :) It will be a great help if you can recommend me with some strong reference.. i.e. links, books etc. – bandejiya Aug 4 '18 at 15:55
3

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.

  • Suppose CPU chooses memory to store variable..Does'nt stack variable will occupy space after they are declared..? And free memory should be decreased in the jack function w.r.t available memory before calling jack function..? – bandejiya Aug 3 '18 at 21:03
  • The CPU doesn't choose anything. The compiler makes that decision. If it decides the variable is only used for a short time and there is a spare register it will choose to use that register instead of allocating space on the stack. Your view of the language and compiler is too naïve. – Majenko Aug 3 '18 at 21:06
  • Sorry..! I am a UG fresher.. still learning.. :).. but let say compiler chose.. ! my query if it chooses memory to store variable..Does'nt stack variable will occupy space after they are declared..? And free memory should be decreased in the jack function w.r.t available memory before calling jack function..?(& Of course if those variables will be used in some conditional operations) – bandejiya Aug 3 '18 at 21:12
  • If the compiler chooses to allocate memory then there are two possible scenarios. Either the memory will be allocated on the stack at the point the function is entered, or it will be allocated when the variable is first used. You should look at the assembly language the compiler produces. – Majenko Aug 3 '18 at 21:14
0

Function 'jack' also does not show any difference in allocation. Since jack() is called only within this one module (known, because the program only consist of one module), and only once, the compiler can replace the function-call with the actual code of the function, saving both the space and time of a call and return.

That optimization is called "inlining" (usually only useful for instructional purposes but learning appears to be the whole point of your sketch). You can suppress the inlining by prefixing both the function and its declaration with __attribute__ ((noinline)). (Note that this attribute is specific to the gcc compilers; other compilers may provide a different way to do it, or none at all):

__attribute__ ((noinline)) void jack();

...

__attribute__ ((noinline)) void jack(){
   ... etc ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.