Welcome

This blog has permanently moved to
https://boseji.com

Contact Form | Email Address

© 2018 All rights reserved by Boseji

Thank you for all your support.

Friday, May 18, 2012

Static Storage Class in C

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.

No comments:

Post a Comment