+5

Implementation of static in c

We say static variable/functions are visible only in the file ,in which these are declared. How it is implemented internaly?

1/12/2019 4:38:49 AM

punnoose

17 Answers

New Answer

+8

There are two types of data segments Initialized data segment and BSS which is assumed as an uninitialized data segments. 1:-> Initialized Data Segment :- Initialized data stores all global, static, constant, and external variables ( declared with extern keyword ) that are initialized beforehand. Data segment is not read-only, since the values of the variables can be altered at run time. This segment can be further classified into initialized read-only area and initialized read-write area. #include <stdio.h> char c[]="Rewa"; /* global variable stored in Initialized Data Segment in read-write area*/ // in read write area we can perform read and write both const char s[]="Sololearn"; /* global variable stored in Initialized Data Segment in read-only area*/ // in constant we only can read value to insert or update constant is not allowed so it is kept in read section. int main() { static int i=11; /* static variable stored in Initialized Data Segment*/ return 0; }

+6

Hi punnoose I will try to explain concept with help of an example. static variable preserve its value in between function calls.  If it is static it will initialize with zero automatically and share same memory location. e.g in 1st function call it will perform operations in a memory location, if 2nd function call then the same memory will use, because of same shareable memory. for example Without static: #include<stdio.h> int fun() {   int count = 0;   count++;   return count; }    int main() {   printf("%d ", fun());   printf("%d ", fun());   return 0; } OUTPUT: 1  1 With static: #include<stdio.h> int fun() {   static int count = 0;   count++;   return count; }    int main() {   printf("%d ", fun());   printf("%d ", fun());   return 0; } OUTPUT: 1   2 In second case, the variable count preserved its value in between the two function calls(i.e, from main the function fun() called two times). https://stackoverflow.com/questions/572547/what-does-static-mean-in-c

+6

static variable may be of an internal or external depends on where it is declared , when a variable is declared as static it's garbage value is removed and initialised to a null value ,the contend will remain through out the program execution . garbage value is initialised at once When the program (executable or library) is loaded into memory, static variables are stored in the data segment of the program's address space (if initialized), or the BSS segment (if uninitialized), and are stored in corresponding sections of object files prior to loading.

+6

2:-> Uninitialized Data Segment (bss) :- Data in this segment is initialized to arithmetic 0 before the program starts executing so every time any data which is uninitialized is initialized in this segment so that's why there is no any uninitialized variable by which it's easy to compilation and execution of the code. Uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to 0 or do not have explicit initialization in source code. So in this way this is worked internally for any static variables. #include <stdio.h> char c; /* Uninitialized variable stored in bss*/ int main() { static int i; /* Uninitialized static variable stored in bss */ return 0; } Here is an link with regards to this may help to get more information. https://stackoverflow.com/questions/9535250/why-is-the-bss-segment-required

+6

~ swim ~ I am just concern about using globals with synchronization with time is important when we are using threads Agreed on changing state of variable is main reason of arising an concurrency issue. if you're never going to reuse the code) the globals are not bad. However if you're ever planning on extending the software because in this case to changing of state can be possible

+5

~ swim ~ agreed fully with you that technically its wrong terminology as well as globals variables should not be used in technology because of issues attched with its uses like:- Implicit coupling Concurrency issues Namespace pollution Memory allocation issues Testing issues But my moto is here to only give an way of understanding the basics first before going further Anyway if you are thinking of some other thing let me know😉

+5

Implicit coupling -- A program with many global variables often has tight couplings between some of those variables, and couplings between variables and functions. Grouping coupled items into cohesive units usually leads to better programs. Concurrency issues -- if globals can be accessed by multiple threads of execution, synchronization is necessary (and too-often neglected). When dynamically linking modules with globals, the composed system might not be thread-safe even if the two independent modules tested in dozens of different contexts were safe These too need to be handle with care

+4

punnoose Variable have something called "linkage specification". There are two types 1. external linkage - means visible everywhere like global variables have 2. internal linkage - visible within a translation unit only. static variable have internal linkage, hence they are not visible outside a translation unit. Due to this you can hava a same variable name of same type in two different C files. static variables are stored in .Data Segment if initialised with value other than 0 0 initialized or default initialized static variables goes into .BSS segment (Block Started by symbol) There ia no such thing as non initalized static variables. They are always initialized to their default values if not explicitly initialized static int i; // goes in .BSS segment, default initialized to 0 static int i=10; // goes into .Data seg with value 10

+4

Rewa Mathur In C it can be difficult to avoid global variables Concurrancy issues - if you are changing the state (i.e. modifying the variables, else global const or enums are fine in C) Memory issues? like? (failure to dellocate is programming error. Memory wise it can be benificial like you can have bigger arrays in global scope than in local scope (old knowledge may/may not be relevant with today's compilers) Testing issue - sure you can have namespace pollution - that's why you have static :)

+3

*Asterisk* thanks😉

+3

Bennett Post By "static external" i guess you mean variables and functions declared in the header file. They are still a part of the same translation unit. There is no such thing as "static external"

+3

~ swim ~ The word "external" here means external to all blocks; as opposed to "static internal variables", objects with static storage class that are local to a block.

+3

Rewa Mathur technically "uninitialized" static or global variables is still a wrong terminology.

+2

Rewa Mathur i do love the way you explain, thanks for that

+2

I am missing one aspect which, I think, the OP is asking about. Linkage and symbol tables. When a source file is compiled down to an object file, it maintains a symbol table. Objects that have external linkage will be marked 'global', object with internal linkage (static external variables and functions) will be marked 'local' or maybe even entirely removed from the symbol table. If several object files are linked together, global symbols can be referenced, local symbols cannot. So, the use of a symbol table in the object file is how external and internal linkage is "implemented".

+2

Bennett Post Ok :) Global static is a better word though :)

+2

Rewa Mathur Concurrency issue arise only if you are changing the state of the variable, otherwise it doesn't matter how many thread access it. Not sure what you are saying about coupling, an example would be helpful variable search goes from inner most scope i.e local to outermost scope i.e global. Hence local variables are more tightly coupled than anything else. Local variable with same name hides global variable.