1

gcc 4.4.2 c89

I am have been re-engineering some one else's source code.

In a function someone has declared some static variables, but doesn't seem to serve any purpose of having them static. I am just wondering if my comment below would be accurate?

static char tempstr[64]; 

For my understanding when declaring static variables inside a function it will retain is scope so acts like a global variable.

Also, if the static variable is declared in global scope, then its scope is limited to the file only.

Many thanks for any suggestions,

ant2009
  • 27,094
  • 154
  • 411
  • 609
  • I bet the function returns tempstr. It is a common desperation move. The static keyword is then very much needed. – Hans Passant Feb 16 '10 at 04:50
  • 1
    Its not always desperation, if you know a string is going to be xx bytes long, the function returning it is protected by some form of mutual exclusion and the caller has no need to modify what is returned ... you can save tickling malloc() hundreds of thousands of times in a loop (especially when mlockall() is in use). But yeah, there's a difference between calculated optimization and someone who just loses track of pointers. – Tim Post Feb 16 '10 at 09:25

4 Answers4

4

If I understand your interpretation, it is accurate.

Inside a function static means "allocate data segment memory so the value persists between function calls and so that all function instances (think, recursion or threads) share the same actual storage."

This matters a lot if a previous value is used in a later function call or if a reference is leaked out of the function by an external call or by returning a pointer.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
2

Yes, your interpretation is correct. Except that be careful with what you call "acts like a global". It acts like a global only in the sense that it retains values between calls, but it's not globally visible, but still only in the function that declared it.

Also see this question.

Community
  • 1
  • 1
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
2

It doesn't make it a global. Its still a local variable it just retains its value during successive calls.

rerun
  • 25,014
  • 6
  • 48
  • 78
1

That's correct. One more aspect to keep in mind is that if your code is multi-threaded, all the threads will share the same copy of the variable, so it is technically possible that the original designer used that variable as a cross-thread communication mechanism. I don't advocate that technique, but I can't rule it out without more information.

David Gladfelter
  • 4,175
  • 2
  • 25
  • 25
  • The application isn't multi-threaded. But what you are saying is that when a different thread calls the function the value will be retained for all threads. So if one thread changes the value then another thread accesses the function will see the modified value by another thread. Which could be a bad thing if you don't want that to happen. However, for cross-communication you might want the threads to read a value that has been set from another thread, maybe to use the modified value for something else. Would I be correct in my statement? – ant2009 Feb 16 '10 at 04:53
  • 1
    yes, that's correct. My problem with static local variables is that they're "state". Maintaining a consistent program state is probably the hardest thing about programming, and when you have little nuggets of state lying around scattered all over the place, it makes it very hard to write a correct program. I prefer to model the problem domain and create a hierarchy of classes that a non-programmer domain expert would recognize as the problem s/he's trying to solve. State represented by static local variables is hard to locate, maintain, and understand. – David Gladfelter Feb 16 '10 at 16:13