Static Variables In Embedded Programming

As a newbie to embedded programming ,Static storage class specifier was one that was difficult to comprehend.Google didn't help me understand much about how these variables are stored in memory.But then collecting a plethora of bits and pieces from google has given me some insight on how these variables work and are stored in memory.So thought this post would help a few.
To begin with the name static comes from the fact that these variables are stored in the same memory location throughout the program execution.This justifies the fact that even if a static variable is declared local to a function it doesn't get destroyed once the control is out of that function.But one would not be directly able to access the variable outside its scope even if it is alive in the memory.But declaring a global pointer to that static variable will enable us to access it anywhere in the program.Consider the eg:

#include <iostream>
using namespace std;

int *c;                             //global pointer for int  

void check()
{
    static int a= 10;
    c=&a;
}

int main()
{
   check();
   cout << "The value is " <<*c<<endl;    
   return 0;


}

output:
The value is 10


In the above code the the static variable declared inside check() is not destroyed once the program is out of the function and hence can be accessed by the global pointer c.Another important fact to note is that global static variables are limited to module or file scope,that is global static variables declared in one file wouldn't be accessible from other files.
In C++ ,we can view static variable in one class as a variable that is shared by all objects created for that class.More interestingly the static variable defined in a class can be used to count the no of objects created for that class as in the below program.

#include <iostream>
using namespace std;

class foo
{
    public:
    static int ObjectCnt;             /*in class initialization                                           of static variable is                                             prohibited*/
    foo()
    {
        ObjectCnt++;
        
    }
    static int blah()
    {
         cout<<"this is function blaah ";
         return 0;
        
    }
     
};

int foo::ObjectCnt = 0;                  /*static member of a                                                class can be                                                      initialized only out                                              of main*/
                                                                                                           /*note that static                                                  variable can be called                                            by calling the class                                              and not the object*/  

int main()
{
  

  foo foo1,foo2;                        // two objects declared

  cout<<"no of foo's  : "<<foo::ObjectCnt<<"\n";

  cout<<foo::blah()<<endl;
}

output:
no of foo's  : 2
this is function blaah  0

Besides static variables static methods can also be defined in a class, these methods are common to all objects in that class and unlike other methods these are referenced using the class name rather than the object name.In the above code blah() is a static method that is called in main.

As a embedded engineer I was more concerned about the memory map of the variables.
I found this resource to be useful:
                                              http://www.inf.udec.cl/~leo/teoX.pdf

In the lower level uninitialized static variables are stored in programs BSS(Block Started by Symbol) segment,this gives a advantage over automatic(normal variables declared inside the function) variables which are stored in the stack upon function calls.

Most of us forget to acknowledge the ability of the embedded compiler(including linker)  to optimize the code that we have written.As a part of optimization majority of the embedded c/c++ compilers store constant static variables in the ROM(flash) and there by reducing the overhead on the stack.I still feel the post is really incomplete,so kindly give your suggestions.

No comments:

Post a Comment