Sasikumar Murugesan
Sasikumar Murugesan

Reputation: 4520

C++, What is difference between static local variable(method) vs global(file) variable?

  1. A local static variable is created once and the method will go out of scope, but the static variable is not destroyed and it will live in memory until the program ends.
  2. A global static variable live in memory until the program ends.

So what is difference between local and global static variables?

When should we use local static variables and global static variables?

Upvotes: 3

Views: 5318

Answers (6)

mustangDC
mustangDC

Reputation: 967

An addition what others have said :

Scope and Lifetime are two different things.

Static variables' lifetime = Program's lifetime, that's why static variable is not destroyed and it will live in memory until program got end.

  1. Scope means where we work with the variable/use the variable
  2. Lifetime on the other hand is the complete execution time of the program, there may be certain cases where we terminate the program but in backgroud still bits and pieces are running.

When we have to use local static variable and global static variable?

  • We should use a local static when we need a variable restricted to that function only
  • We should use a global static when we need a variable common to all functions in the program. Eg:- A variable which contains the Budget of the organisation, whic is same for all departments

Hope this helps. Thanks

Upvotes: 2

Tas
Tas

Reputation: 7111

Other answers have told you the differences between a local static object (local to a function) and a non-local static object (static objects declared global or in a namespace); however, you should also understand the importance of using one over the other.

The information I use here comes from Effective C++ Third Edition by Scott Myers (a recommended and excellent read)

A static object is one that exists from the time it's constructed until the end of the program.

As others have mentioned, a non-local static object is constructed before main whereas a local static object is constructed the first time the function is called.

But what if you had two static objects and one relied on the other. How could you be sure that one would be constructed before the other? You can't: The relative order of initialisation of non-local static objects defined in different translation units is undefined

Scott Myers definition of a translation unit (from aforementioned book):

A translation unit is the source code giving rise to a single object file. It's basically a single source file, plus all of the #include files.

So imagine you have these two non-local static objects in separate source files, you couldn't guarantee one would be constructed before the other. This is where a local static object prevails!

Consider Scott Myers' example of a FileSystem class and a Directory class, where the Directory relies on the FileSystem:

class FileSystem
{
public:
    size_t numDisks() const;
};
extern FileSystem tfs;

and

class Directory
{
public:
    Directory()
    {
        size_t disks = tfs.numDisks();
        ...
    }
};

If Directory gets constructed before FileSystem, then you are going to be using an uninitialised class! Fortunately, you can get around this by using local static objects!

class FileSystem
{
public:
    size_t numDisks() const;
};
FileSystem& tfs()
{
    static FileSystem fs;
    return fs;
}

Now when Directory is constructed, even if it's constructed before FileSystem is, FileSystem is guaranteed to be constructed before it's used!

Upvotes: 3

Nishant
Nishant

Reputation: 1665

A local Static is visible only inside the function, it has the scope of function level only, while Global Static has the program scope. And initialization, global is initialized at before main, local static would be initialized the first time function will get called.

Global Static variables if not used properly can cause issues, because in a large program it would be hard to keep track what is changing it's values.

Another possible drawback may be the linkage, what if you are using a global static variable named varnew and you are using an external library which also have the same variable.

It is always suggested to limit the scope of your variables to avoid unnecessary errors.

Upvotes: 0

mksteve
mksteve

Reputation: 13073

A local static is visible only within the function. This allows the name to be kept secret. This could be used to implement profiling logic, where the number of calls for each function are tracked.

The more significant benefit of a local static, is the point in time when it is constructed. Global data is constructed before main is called. This occurs from the top of the file to the bottom of the file for each C++ file in the program, but the order of files in the construction is undefined. A local static is only constructed the first time it is called (C++ 11 guarantees only once, earlier C++ requires some synchronization if it can be called multi-threaded.) This means a complex object which requires some facility of your program to be initialized (e.g. some database) then this can be controlled as you can perform that initialization in main before your functions containing these static variables are called.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206567

Differences I can think of:

  1. The scope of the variable. A global static variable is visible to the entire program. A function static variable is visible only in the function.

  2. Initialization. A global variable is initialized before main is executed. The function static variable is initialized when the function is called the first time.

Upvotes: 1

phoog
phoog

Reputation: 43036

Global static variables can be accessed, and potentially changed, by other scopes. For example, two functions can share state using a global static variable. A local static variable is just that: local. Other functions cannot see it directly.

The fact that global static variables may be changed by other functions is a primary reason why they are so dangerous. Programmers often make unwarranted assumptions about the variable. For example, if a method uses, but does not modify a global static variable, it is still possible for the value to change from the beginning of the method to the end. People will tend to assume, however, that the value is unchanging.

Upvotes: 0

Related Questions