Tuesday 2 April 2013

Summary of Storage Duration /Scope/ Linkage of variables/stoarge area and storage class specifier

Storage class specifier,storage duration ,scope and linkage and storage area of variables are the related concepts .But there is very necessary to understand them distinctly.Storage class specifier declares that a variable or function declared in a program where stored.There are following types of storage specifier in C :-

1)auto
2)register
3)extern
4)static
5)typedef
6)__declspec
7)mutable
8)thread_local


(1)auto:-auto is a keyword used to declare an automatic storage class.It is declared as following types :-

auto  a=2;              //auto
auto  b=3.8;            //float
auto c='h';            //char

 auto  b1=3.8f;          //float 
 auto  b2=3.8F;          //float
 auto  b3=3.8l;         //double
 auto  b4=3.8L;          //long double 
 printf("a=%d\n",a);    //int 
printf("b=%f\n",b);    // float
printf("c =%c\n",c);   //char

2)resister:-resister is also a storage class used to faster access of variable because the storage will be done in CPU registers .It is a hint to the compiler that the variable will be heavily used and that  recommend it will be kept in a processor register if possible.We can declare a datatype register in following manner:-

register int ri=98;

3)extern:-extern is also a storage class specifier .It is used for the use a variable which is initialized in another file in same project.When we use extern modifier with any variables it is only declaration i.e. memory is not allocated for these variable.We can declare register keyword in following manner:-

extern int ei=23;

4)Static:-static is also a storage class specifier used in C/C++.It also stores variable in memory.The default initial value is zero.The value of the variable persists between different function calls.We can use a static datatype  in following manner:-

  

#include <stdio.h>

void func() {
        static int x = 0; // x is initialized only once across three calls of func()
        printf("%d\n", x); // outputs the value of x
        x = x + 1;
}

int main(int argc, char * const argv[]) {
        func(); // prints 0
        func(); // prints 1
        func(); // prints 2
        return 0;
}


(5)typedef:-typedef keyword provide us to rename a variable datatype into a short and meaningful way.
Ex:-
                typedef unsigned long int ULI;
                 ULI a;



13)_declspec:-It is a storage class modifier  used with many extended attribute like dllimport and dllexport.



7)mutable:-This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.

mutable member-variable-declaration;

8)thread_local:-The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration. thread_local can only be declared for global variables, plus those declared with static or extern.It is declared as following:-


 thread_local unsigned int rage = 1



storage duration explain how much duration a variable exist.There are four types of storage duration:-

1) Automatic storage duartion:-In this type of storage duration the variables are allocated at the beginning of enclosing code block and de-allocated at the end.All non-global variables have this storage duration expect those declared static ,extern or thread_local .

2)Static storage duration:-The variable is allocated when the programs begins and de-allocated when the program ends.Only one instance of  the variable exists.All global variable have this storage duration & also which declared as static or extern.

3) Thread:-The variable is allocated when the thread begins and de-allocated when the thread ends .Each thread has its own instance of the variable .Only variable declared as thread_local have this storage duration.
Thread_local can only be declared for global variables and  those declared as static or extern.

4)dynamic:-The variable is allocated and de-allocated per request by using dynamic memory allocation execution.


Linkage:- linkage refers to the ability of a function or variable to be referred to in other scopes .If a variable or function with the same identifier is declared in several scopes ,but cannot be referred to from all of them,then several instances of the variable are generated .The following linkage are recognized:-

(i))No linkage:-The variables can be referred to only from the scope it is in .All variables with automatic ,thread and dynamic storage duration have this linkage.

(ii) Internal linkage:- The variable can be referred to from all scopes in the current translation unit. All variables with static storage duration which are either declared static, or const but not extern, have this linkage.

(iii)External linkage:-The variable can be referred to from the scopes in the other translation units. All variables with static storage duration have this linkage, except those declared static, or const but not extern.

Scope:-Scope of the variables defines the when and where the variable or function will be available and what was their meaning?There are basically four type of scopes available:-

i)function scope:- This only applies to labels ,whose names are visible throughout the function where they are declared ,irrespective of the block structure .No two labels in the same functions may have the same name because the name only has a function scope ,the same name can be used for labels in every function .Labels are not objects -they have no storage associated with them.

ii)File scope:-The name declared outside the function has file scope which means name is usable at any point from the declaration on to the end of the source code  file containing the declaration .It is possible for these names to be temporarily hidden by declaration within compound statement .A name introduced by by any function defintion be always file scope because function definition must be outside other functions.

iii)block scope:-A name declared inside a compound statement or as a formal parameter to a function has block scope and is usable up to the end of enclosing braces.Any declaration of a name within a compound statement hides any outer declaration    of the same name until the end of the compound statement .

iv)Function prototype scope:-In the function prototype scope,declaration of a name extends only to the function prototype.


wrong declartion:-
                                  void func(int i, int i);

correct declaration:-
                                   void func(int i, int j);



Storage area is the space where the variables stored.We have some important information  about storage area .There is basically three storage area :-


(i)Code Segment(CS)
(ii)Data Segment (data, bss)
(iii)Stack Segment (heap,stack)


C program instructions get stored in code/text segment
Register variables are stored in Register. 
The memory created dynamically are stored in Heap.
Local Variables/arrays (Except static) are stored in Stack.  
 Global, extern & static variables/arrays are stored in data segment.

 a) Uninitialized static/global/extern variables are stored in the BSS(Block started by symbol) of the data     segment .ie high address..
               (Note: Uninitialised static variable is automatically initialised to zero)
               (Note: Uninitialised global variable is automatically initialised to zero)
               (Note: Uninitialised extern variable is automatically initialised to zero)
 b) Initialized static/global/extern variables are stored in data section of the data segment. ie low address
               (Initialised static also includes zero initialised static variable)

const local variable(initialised) is stored on stack.
const local var (uninitialized) is NOT POSSIBLE. It must be initialized when declared.
const global var (initialized) is stored in data section of data segment.
const global var (uninitialized) is NOT POSSIBLE. It must be initialized when declared.
-----------
null pointer ->   .bss
static pointer (uninitialised)-> (initialized to null by default) -> null pointer ->   .bss
command line arguments (stack)
function parameters (stack)

----------
Data in the bss segment is initialized by the kernel to zero before the program starts executing.
------------
Default storage class specifiers:
For external declarations (outside a function) the default storage class specifier will be extern and for internal declarations(inside a function) it will be auto.

default storage class specifier for functions  is always extern.
We can understand the variable's storage area with the help of these snapshots:-



Objects:-There are basically two type of objects in C:-the internal and external objects.

Anything declared outside a function is external object,
Anything inside a function, including its formal parameters, is internal.

At the outermost level, a C program is a collection of external objects.

The exact meaning of each storage-class specifier depends on two factors:-

Whether the declaration appears at the external or internal to a function.

Whether the item being declared is a variable or a function


No comments:

Post a Comment