Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'global' and 'static global'

A global variable's scope is in all the files, while a static global variable's scope is just the file where it is declared. Why so?

Where are global or static global variables stored in memory?

like image 591
TwiggedToday Avatar asked Jun 06 '09 15:06

TwiggedToday


People also ask

What is the difference between static & global?

The difference between a static variable and a global variable lies in their scope. A global variable can be accessed from anywhere inside the program while a static variable only has a block scope.

What is difference between static global and non static global?

A static global variable has internal linkage. A non-static global variable has external linkage.

What is static global?

A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope. Constant Variables. In C, the preprocessor directive #define was used to create a variable with a constant value.

What is the difference between global int and static int?

Global vs Static Variables They have external linkage, which means that in other source files, the same name refers to the same location in memory. Static global variables are private to the source file where they are defined and do not conflict with other variables in other source files which would have the same name.


2 Answers

There is some confusion, since static in C can mean two different things. One is static storage duration, and the other is internal linkage. static used as a keyword on file-scope will give the function or object it is used with internal linkage.

Internal linkage for a function or object means that if you declare another function in another "file" (this is not really called "file", but rather translation unit - TU), then that declaration will refer to a different function: The name declared in that unit will "link" to a different entity than the name declared in that other translation unit, which was "internal" to that TU. The same applies to objects.

Whether or not a file-scope variable is declared with static, it will still have a static storage duration: That means it lives through the whole program, and dies when the program terminates. Another example of an object that has static storage duration is a string literal. Where objects that have static storage duration are stored isn't specified, but they are usually stored depending on whether they are initialized or not: Initialized file-scope variables are usually stored in a section called ".data", while non-initialized file-scope variables are usually stored in a section called ".bss". Remember that if the variable isn't initialized, it will be zero initialized at the start of the program: The ".bss" section is usually zero initialized by an implementation on program's startup.

I said "usually" everywhere, since where things are stored isn't specified. For example, some implementations could store string literals in a read-only section. And if you have a file-scope pointer and don't initialize it, the implementation initializes it to a null-pointer, which is not necessarily an object with all null bytes :)

like image 176
Johannes Schaub - litb Avatar answered Sep 28 '22 01:09

Johannes Schaub - litb


They're both stored in the data segment; the difference is that the global has an externally-visible linker symbol, and the static global does not.

like image 37
Dave Avatar answered Sep 28 '22 02:09

Dave