Storage class specifiers:
A storage class specifier in a declaration modifies the linkage of identifiers declared and the storage duration of the corresponding objects.
Identifier : A identifier is a property which is to be obeyed by the name of the functions and variables.
Eg:
A function or variable name cannot start with anything other than alphabets or underscore sign
Fun, _data are valid identifiers where as 1data is an invalid identifier.
A identified is case sensitive.
DATA and data are different identifiers.
The length of the identifier should be according to the specification given by the compiler vendor.
Linkage:
Linkage is a property of identifier. There are three kind of linkages an identifier can have – Internal Linkage, External Linkage, and No Linkage.
An identifier that is declared in several translation units or several times in the same translation unit, may refer to the same function or object in each instance.
A translation unit or compilation unit is a file under compilation.
The extend of an identifiers identity in and among several translation unit is determined by the identifiers linkage. Identifiers in C can have external, internal or no linkage.
The linkage is determined by the declaration’s position and the storage class specifier if any. Only objects and function identifiers can have external or internal likage.
External Linkage :
Function and object identifiers declared with the storage class specifier ‘extern’ have external likage with one exception. If an identifier has already been declared with ‘internal linkage’ , a second declaration with in the scope of the first cannot change the identifiers linkage to external. The compiler treats function declarations without a storage class specifier as if they have included the specifier ‘extern’. Similarly, any object identifier that you declare outside all functions and without a storage class specifier has external linkage.
Internal Linkage:
An identifier with internal linkage represents the same object or function within a given translation unit. This identifier is not presented to liker. A function or object identifier has internal linkage if it s declared outside all functions and with storage class specifier ‘static’. However, if a given identifier is declared with external linkage in any translation unit, you cannot declare the same identifier with internal linkage in the translation unit.
No Linkage :
All identifiers that have neither external nor internal linkage have no linkage.
Eg:
1.Identifier that are not the name of variables or functions, such as label names, structure tags, typedef names.
2. Function Parameters.
3. Object identifiers that are declared within a function and without storage class specifier extern.
Storage Duration of Objects :
During the execution of the program, each object exists as a location in memory for a certain period, called its lifetime. There is no way to access the object before or after its lifetime.
In C, lifetime of an object is determined by its storage duration. Objects in C have 3 kinds of storage duration : Static, Automatic, Allocated.
C does not specify how objects must actually be stored in any particular system architecture, but typically, objects with static storage duration are located in data segment of program in memory, while objects with automatic storage duration are allocated in stack.
Static Storage Duration:
Objects that are defined outside all functions, or within a function and with the storage class specifier static, have static storage duration. These include all objects whose identifiers that have internal or external linkage. All object with static storage duration are generated an initialized before the execution of the program begins. Their lifespan is throughout the program.
Automatic Storage Duration:
Objects defined within a function with no storage class specifier have automatic storage duration. Function parameters also have automatic storage duration.
The lifetime of an automatic object is delimited by the braces { …}.
Allocated :
Allocated storage duration is for memory allocated from heap for dynamic memory. Here memory is ALLOCATED for specific period of time between the malloc() / calloc() and free().
Initialization:
An object defined without an initialize either has an undetermined initial value or implicitly initialized by the compiler.
Implicit Initialization:
Automatic – Garbage value
Static – Zero
Pointer Object – NULL
Explicit Initialization:
The initialize for object with static storage duration must be constant expressions.
Automatic objects are not subject to this restriction.
Declaration that is not a definition, such as the declaration of an external variable must not include an initialize.
Eg. Extern int n;
Friday, November 7, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment