8

Lets say you have a class (c++) or module (single c file). Then in one of your functions you want to store a copy of a variable and hold its value until the next time the function is called, is it better to have a global (could be private in c++ and not extern'd in c to keep it in the module scope) or make a local static variable?

e.g.:

void some_func_that_does_not_do_anything_useful(int arbVal)
{
    static int lastArbVal = 0;

    if (arbVal > lastArbVal)
    {
        lastArbVal = arbVal;
    }
}

The reason I would make a static is to keep its scope as limited as possible, but certain things I read suggest you should use globals for this, so now I am confused.

What is best (if any)?, pros/cons?

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • 2
    If you only use it in that function, and nowhere else, then making it `static` is probably the best solution. Though you might want to think about cases like multi-threading, as the static variable will be shared between all threads. – Some programmer dude Oct 17 '13 at 08:32
  • In the interest of encapsulation a local static would be best. This is also the approach many C functions take. `strtok` comes to mind. – Adam Oct 17 '13 at 08:32
  • 1
    To quote from the `strtok` man page: "Never use these functions". Let's not use that as an example of how to do things :-) – Philip Kendall Oct 17 '13 at 08:38
  • Can you share the resource/s from where you read that you should use globals in this case? – Vandesh Oct 17 '13 at 08:39
  • No the example shows pretty much how I have it in my code. Its all within this one function and not used anywhere else :) – code_fodder Oct 17 '13 at 09:32
  • What purpose of the lastArbVal variable? – zabulus Oct 17 '13 at 09:55
  • @zabulus in this example (which is pointless) nothing much. In my real code "lastArbVal" would remember that last value so that I can find the difference between the current value and the previous value and do actions accordingly – code_fodder Oct 17 '13 at 10:21
  • If the function need to be accessed by multiple thread, another benefit of static function level variable is the C++ standard define it as thread safe, and most compiler have option to make it thread safe. – ZijingWu Oct 17 '13 at 10:22

1 Answers1

10

The rule here is simple: if the variable needs to be accessed by more than one functions, make it global. If not, static variables inside functions are usually better. One of the pros is it avoids polluting the global namespace.

Note that if a global variable doesn't need to be accessed outside the file, it's better to declare it as file-scope variable(i.e, declare it as static)

Back to your example, I think it's best to use static variable like you already did.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    An alternative to file-scope `static` is an anonymous namespace. – juanchopanza Oct 17 '13 at 08:58
  • 2
    One more reason file scope `static` might not be suitable is static initialization order. If the function that uses it is called during initialization of other statics from different translation units, then you're in for trouble. – jrok Oct 17 '13 at 09:04