Use of static variables and functions in global scope
Asked Answered
D

4

23

Is there a use for flagging a variable as static, when it lies in the global scope of a .cpp file, not in a function?

Can you use the static keyword for functions as well? If yes, what is their use?

Descant answered 18/1, 2011 at 14:29 Comment(1)
Sounds like someone calling anything homeworkDescant
A
19

In this case, keyword static means the function or variable can only be used by code in the same cpp file. The associated symbol will not be exported and won't be usable by other modules.

This is good practice to avoid name clashing in big software when you know your global functions or variables are not needed in other modules.

Anglim answered 18/1, 2011 at 14:32 Comment(1)
In C++ one should use unnamed namespaces.Concede
M
20

Yes, if you want to declare file-scope variable, then static keyword is necessary. static variables declared in one translation unit cannot be referred to from another translation unit.


By the way, use of static keyword is deprecated in C++03.

The section $7.3.1.1/2 from the C++ Standard (2003) reads,

The use of the static keyword is deprecated when declaring objects in a namespace scope; the unnamed-namespace provides a superior alternative.

C++ prefers unnamed namespace over static keyword. See this topic:

Superiority of unnamed namespace over static?

Misdo answered 18/1, 2011 at 14:32 Comment(4)
And the latest C++0x draft undeprecates it.Rest
@Fred: amusing, it changed between n3092 and n3225, do you know what motivated this change ?Concede
@Matthieu: that's even more interesting. Can you please tell us what motivated this change? or at least refer us to a link?Misdo
I could not find anything really relevant, I've asked the question ( stackoverflow.com/questions/4726570/… ), let's hope someone on SO knows something about it.Concede
A
19

In this case, keyword static means the function or variable can only be used by code in the same cpp file. The associated symbol will not be exported and won't be usable by other modules.

This is good practice to avoid name clashing in big software when you know your global functions or variables are not needed in other modules.

Anglim answered 18/1, 2011 at 14:32 Comment(1)
In C++ one should use unnamed namespaces.Concede
M
1

Taking as an example -

// At global scope
int globalVar; // Equivalent to static int globalVar;
               // They share the same scope
               // Static variables are guaranteed to be initialized to zero even though
               //    you don't explicitly initialize them.


// At function/local scope

void foo()
{
    static int staticVar ;  // staticVar retains it's value during various function
                            // function calls to foo();                   
}

They both cease to exist only when the program terminates/exits.

Marquittamarr answered 18/1, 2011 at 14:43 Comment(2)
But does the function scoped static variable get initialized at runtime or only when its scoped function, in this case foo() is ran?Karankaras
@Marquittamarr Is not globalVar a non static if you don't specify static by default?.I think one can easily extend the non static global variables.Jug
L
0

Static at the global scope means that a variable or function is only visible to the translation unit where it's defined. Static can also be used for local variables, which means they will be placed in static memory.

For example, consider a project with two .cpp files: x.cpp and y.cpp. Both files have a global variable called var. When the linker tries to do its job and form an executable file, it won't be able to resolve the ambiguous reference to "var". If one or both of the "var" variables were marked static, that variable would not be linked to the other file. You can think of it like declaring the "var" as private to the file.

In a local scope, static says a variable will be initialized once at compile time and placed in static memory. This means that it will retain its value over multiple function calls. Contrast this with a standard local variable, where a new instance is pushed onto the stack with each function call.

Hope this helps. For a more in-depth explanation check out this: https://www.youtube.com/watch?v=g4Dn2cwSrC4

Landis answered 21/3 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.