A small request for C programmers: no static locals

December 9, 2010

I have a small suggestion for C programmers: please don't use static function local variables. Yes, I know, they've been part of C for a very long time, they are kind of nifty, and they're sometimes nice. But still, please use a file level static variable instead, which has just the same effects.

Well, almost. There is one important difference; in practice, many more tools can find and inspect file level static variables than can find and inspect function static variables. The result is that file static variables are actually accessible, while using static locals is a great way of making bits of state more or less invisible unless people try hard. This is an especially important (or bad) thing if you are building a system that is supposed to be observable and debuggable so yes, Solaris kernel, I am looking at you.

(In theory tools could do just as good a job with function static locals, since both file statics and function statics ultimately map to storage locations. All you need is symbol table entries, a suitable naming convention, and some elbow grease and programming. In practice, well, at least one of these ingredients is usually skipped.)

The reverse corollary is also true. If you are building something like SystemTap, DTrace, or a debugger or disassembler on a system where C's static locals make it into the symbol table, please invent some syntax for talking about them (if necessary) and then let people use it in appropriate places.

(If static locals do not make it into the symbol table and you have the ear of appropriate people, you can start working to change that.)

Written on 09 December 2010.
« Exploring a limitation of the DTrace fbt provider (on x86)
How to get sysadmins to never use your software again »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Thu Dec 9 01:08:52 2010
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.