Tag Archives: static

static keyword C CPP

static keyword C CPP

There is a big difference between static functions in C and static member functions in C++. In C, a static function is not visible outside of its translation unit, which is the object file it is compiled into. In other words, making a function static limits its scope. You can think of a static function as being “private” to its *.c file (although that is not strictly correct).Global static variables can be called only inside that file.With Static variable even “extern” can’t access it.

In C++, “static” applies to member functions and data members of classes.

why static functios would be required ?

You may want to use the function without an object instantiated. Also if the function is called from another static function it must be stat. The most typical example of this is a builder function like for singleton class.

Static class in java:



A static data member is also called a “class variable”, while a non-static data member is an “instance variable”. This is Smalltalk terminology. This means that there is only one copy of a static data member shared by all objects of a class, while each object has its own copy of a non-static data member. So a static data member is essentially a global variable, that is a member of a class.

Non-static member functions can access all data members of the class: static and non-static. Static member functions can only operate on the static data members.
One way to think about this is that in C++ static data members and static member functions do not belong to any object, but to the entire class.

A static member function cannot be declared with the keywords virtual, const, volatile, or const volatile.

A static member function can access only the static members, enumerators, and nested types of the class in which it is declared. Suppose a static member function f() is a member of class X. The static member function f() cannot access the non-static members of X or the non-static members of a base class of X.

A static member function does not have a “this” pointer. So “this ” pointer can’t be used in a static function.

Same name static var can exist in 2 functions since  during compile time these would be treated as different symbols.

Static members are not stored n Stack rather they are stored in data Segment and BSS based on if its initialized or un-initialized. Check memory layout for further information.

static keyword is applied to struct as well. A static variable in a structure can be accessed with struct name but for other variables it has be accessed after creating its object.

With static keyword for a data , the variable persists until the program ends.

For variables declared outside a function scope, static changes the visibility (linkage) of the variable. The variable will be a global variable since it is outside a function scope. If it isn’t static, it will have universal linkage (visibility) so any code linked together with this can access it (they may have to declare it extern). If the variable is outside a function scope and is static, it still a global variable in that it always exists and keeps its value, but no code outside the same compilation unit (that .c file and any .h’s included) can access it.

For variables declared inside a function scope, static changes the location where the variable is stored. If it is not static, it will be an automatic variable, that means it disappears as the function exits and comes back into existence (on the stack) when the function is entered again. It loses its value when you exit the function. And also any references to it (pointers to it) are invalid after the function exits. If a variable declared inside a function scope is static, then it makes it not an automatic variable but a globally allocated one. So the variable will exist after the function exits and thus will keep its value across invocations of the function and also any references (pointers) to it are valid even after the function exits. Note that in both cases the scope of the variable is only within that function so it’s not possible to access it directly (but only via saved reference) from outside the function scope.

One last thing static does is it changes when the initializer (i.e. int test= 1) for the variable is run. For all the cases where the allocation is global (every case except the automatic one), the initializer is run only once, at the beginning of the execution of your program. It is run before main() is even run, so you can get some unexpected results if your initializer isn’t just a constant number but runs some code. For the automatic case, the initializer is run every time the function is entered, and in this case it is always after main() has been entered.

Can we have a static virtual functions?

No, because it doesn’t make any sense in C++.Virtual functions are invoked when you have a pointer/reference to an instance of a class. Static functions aren’t tied to a particular instance, they’re tied to a class. C++ doesn’t have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.

Can we have a static constant functions?

The reason for this is that a const (or volatile or virtual) static method wouldn’t make sense . For example, const implies you can’t modify the object’s members, but in the case of static, there’s no object to talk about it belongs to class not object.

What makes a static variable initialize only once?

It does normally translate into an implicit if statement with an internal boolean flag. So, in the most basic implementation your declaration normally translates into something like
void go( int x ) {
static int j;
static bool j_initialized;

if (!j_initialized) {
= x;
= true;

On top of that, if your static object has a non-trivial destructor, the language has to obey another rule: such static objects have to be destructed in the reverse order of their construction. Since the construction order is only known at run-time, the destruction order becomes defined at run-time as well. So, every time you construct a local static object with non-trivial destructor, the program has to register it in some kind of linear container, which it will later use to destruct these objects in proper order.The actual details depend on implementation.

When it comes to static objects of “primitive” types (like int in your example) initialized with compile-time constants, the compiler is free to initialize that object at startup. You will never notice the difference. However, if you take a more complicated example with a “non-primitive” object
void go( int x ) {
static std::string s = “Hello World!”;

then the above approach with if is what you should expect to find in the generated code even when the object is initialized with a compile-time constant.

Avoid mangling of function name in c++ ?

extern “C” void foo ();
This prevents the C++ compiler from mangling the function name, which would change the function’s name from foo to a different funny-looking name that encodes extra information about the function. A C compiler will not mangle names; it will use whichever name you give to your function or variable.