Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A static variable and a global variable both reside in data segment. Still, static variable has scope limited. Why?

Tags:

c

In a typical C program, both static variable and global variable reside on the data segment. Still the scope of static variable is limited to file. On the contrary, the global variable can be accessed from anywhere. Why does it happen, although both reside in the same memory?

like image 540
dexterous Avatar asked Sep 03 '13 11:09

dexterous


People also ask

Why static variables are stored in data segment?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

What is the scope of a static global variable?

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. In C, the preprocessor directive #define was used to create a variable with a constant value.

Do static variables have scope?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

What is the scope of static variable where is it located?

The scope of the static local variable will be the same as the automatic local variables, but its memory will be available throughout the program execution. When the function modifies the value of the static local variable during one function call, then it will remain the same even during the next function call.


2 Answers

By design.

static at global scope is the keyword you use to mean "I want these variables limited in scope; I do not want to have to care what other modules have declared variables of the same name." The reason using this keyword does a different thing to not using it is in fact exactly the reason for its existence.

Note the keyword means different things in different contexts; at function scope static means "the contents of this variable should persist between function calls".

The actual arrangement of data in memory that results is an implementation detail, and will vary between compilers and platforms.

like image 69
moonshadow Avatar answered Nov 15 '22 21:11

moonshadow


Why does it happen, although both resides in the same memory?

Short answer:-

From the C11 standard ( 6.2.2 Linkages of identifiers) para 4 :

If the declaration of a file scope identifier for an object or a function contains the storageclass specifier static, the identifier has internal linkage.

Internal linkage means that its visible only inside its translational unit.

Detailed answer:

A global variable(without static) has external linkage which means it is visible to other translational units.

When you declare static variables with file scope it has internal linkage but when you declare it with block scope it has no linkage.

Lets understand few terms specifically .( inspired from C keywords (static))

A C variable has one of the following linkages:

  • no linkage :- Variables with block scope have no linkage.It means they are private to the block in which they are defined . All variables with automatic, thread and dynamic storage durations have this linkage, as well as variables declared static at block scope. A variable with file scope can have either internal or external linkage.
  • internal linkage :- The variable can be referred to from all scopes in the current translation unit. All variables which are declared at file scope have this linkage, including variables declared static at file scope.
  • external linkage :- The variable can be referred to from any other translation units in the entire program. All variables which are declared either extern or const with no explicit storage-class specifier, but not static, have this linkage.

e.g-

int i = 5; // file scope, external linkage
static int j = 3; // file scope, internal linkage
...
...
int main()
{
...
...
}
int func ()
{
static int num;// block scope – no linkage
. . .
}

By declaring a variable static on file level (static within function has a different meaning) you forbid other units to access it, e.g. if you try to use the variable inside another unit (declared with extern), linker won't find this symbol.

Emphasis mine :)

like image 37
0decimal0 Avatar answered Nov 15 '22 21:11

0decimal0