Uses of static variables inside functions?

Up vote 2 down vote favorite share g+ share fb share tw.

I have been writing C code for many years, but I recently came accross a feature that I have never used: a static variable inside a function. Therefore, I was wondering what are some ways that you have used this feature and it was the right design decision. E.g.

Int count(){ static int n; n = n + 1; return n; } is a BAD design decision. Why? Because later you might want to decrement the count which would involve changing the function parameters, changing all calling code, ... Hopefully this is clear enough, thanks!

Void first_call() { static int n = 0; if(!n) { /* do stuff here only on first call */ n++; } /* other stuff here */ }.

5 This is the primary reason I use this feature. Take care though; the code is neither re-entrant nor thread-safe, so don't expect it to fix race issues if more than one caller is capable of issuing the first call. – San Jacinto Dec 20 '10 at 18:58 Good comment, duly noted – SiegeX Dec 20 '10 at 19:00 Note that the pthread_once interface provides the equivalent functionality in a thread-safe manner.

It's even safe if the thread calling the initialization function gets cancelled before initialization is finished. (And there's no reason pthread_once can't also be used in a single-threaded program that never expects to use threads. ) – R.. Dec 20 '10 at 20:37 @R.. Thanks!

Glad you were able to shine some light on that. – San Jacinto Dec 20 '10 at 20:53.

I have used static variables in test code for lazy initialization of state. Using static local variables in production code is fraught with peril and can lead to subtle bugs. It seems (at least in the code that I generally work on) that nearly any bit of code that starts out as a single-threaded only chunk of code has a nasty habit of eventually ending up working in a concurrent situation.

And using a static variable in a concurrent environment can result in difficult issues to debug. The reason for this is because the resulting state change is essentially a hidden side effect.

Not sure why this was downvoted. I think this is the best answer so far. – Justin Spahr-Summers Dec 20 '10 at 19:10 @Justin, Thanks - In my first instance of the answer is used words like "always" and was probably too adamant about it being a bad idea ... I do feel that way, but there are usually (always?

:) exceptions to the rule. – Mark Wilkins Dec 20 '10 at 19:12.

I have used static variables as a way to control the execution of another thread. For instance, thread #1 (the main thread) first declares and initializes a control variable such as: /* on thread #1 */ static bool run_thread = true; // then initialize the worker thread and then it starts the execution of thread #2, which is going to do some work until thread #1 decides to stop it: /* thread #2 */ while (run_thread) { // work until thread #1 stops me }.

This will spectacularly not work in weird and hard-to-diagnose cases, as static variables are not guaranteed to be thread-safe. You'd have to use something like a pthread_mutex to ensure consistent reads and writes. – Justin Spahr-Summers Dec 20 '10 at 19:09 On my example only the first thread is allowed to change the state of run_thread.

– karlphillip Dec 20 '10 at 19:13 You still have bugs with potentially unordered memory writes. Short of special guarantees from the implementation or C1x _Atomic types, the only portable way to get the necessary memory barrier is with the pthread_mutex_* functions. – R.. Dec 20 '10 at 20:40.

There is one prominent example that you very much need to be static for protecting critical sections, namely a mutex. As an example for POSIX threads: static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mut); /* critical code comes here */ pthread_mutex_unlock(&mut); This wouldn't work with an auto variable. POSIX has such static initialzers for mutexes, conditions and once variables.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions