Friday, May 18, 2012

There is something special about the usage of Static type in C programming. We were caught unaware of this and tried to fix the issue, that was the time when we really learned the usage of static in its true sense.

We came across a bug in the firmware for LATHI project. The bug surfaced in the display driver for the Nokia5100 LCD. The display buffer was a normal Auto class variable. In case when there was an emergency situation detected the display driver control is taken over by the corresponding Panic mode task. Due to the inability to use the UI task all the base functions of the GUI have been partially implemented for Panic mode. This means that the Driver context would be altered when it is in use by Panic mode.
While exit from the Panic mode into the normal schedule the display would get corrupted.  In order to avoid this every time on each exit from Panic mode, the display was being completely reinitialized in the code. Hence it was not possible to return to the same view that was before the Panic mode entry.

There were two solutions for us -
1. Rewrite the display driver & UI task to support context saving.
2. Save display state before processing the Panic Mode task.

Both solutions had their merits as well as demerits. We tried out both of them and still the result was the same. This was not good we were not even close to the solution.

Then while debugging we found that the display buffer was being accessed by one of the higher level driver functions.
Although the higher level function was in a separate file we were still able to make direct access using `extern`. This was the cause of the real problem.
We modified this high level function and made the display buffer as `static` solving the real problem.

Thus we would like to summaries when static class becomes imperative. So that you can avoid the problem that we diagnosed.

Here are a few points from Wikipedia:
  • In the C programming language, static is used with global variables and functions to set their scope to the containing file. 
  • In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. 
  • While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.
 Now detailing the Usage scenarios for Static in C:
  1. Static for Global Variables:
    •  When the global variables need to accessible only in one file
    • Compiler would guarantee that the variable has been declared and not omitted during optimization
  2.  Static for Local Variables:
    • When the values of the variable needs to be retained for every function call. They need file scope and not be allocated in the stack like usual local variables.
    • Useful while saving context of who called the function before the present callee.
    • Also useful for unique ID generation and pointer arithmetic
  3. Static for Functions:
    • The use of static in functions make sure that the function only has the file scope
    • This function even if declared in the header file cant be accessed by other files unless the function is explicitly defined in that file.
    • Very helpful in implementing local Fix in software.
It is essential to note that the behavior of Static in languages such as C++ Java and C# does not comply to the above said use cases.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to Boseji's Blog

Popular Post

Blogger templates


Creation by Electronics Hobbyists from Bharat(India) to encourage & help people. This blog hosts the numerous innovations, discoveries and projects we provide.
Our Research Wing Adhar Labs
Contact Form | Email Address
We Support Open Source Hardware Definition v1.0
Creative Commons License
All works on this blog are licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at and

Permissions beyond the scope of this license may be available at
Thank you for all your support.

© A.D.H.A.R Labs Research 2008-2013 . Powered by Blogger.

Follow by Email


PCB Unit Converter


- Copyright © Electronics for Bharat -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -