Sorting the Confusion between Storage Classes, Linkage, Duration & Scope
Back to C ProgrammingThere are four distinct ideas that we need to master:
- The Duration of an object describes whether its storage is allocated once only, at program start-up, or is more transient in its nature, being allocated and freed as necessary.
- Scope describes where an identifier has meaning and what meaning it has. The scope of a declaration is the part of the code where the declaration is seen and can be used.
- Linkage is used to determine what makes the same name declared in different scopes refer to the same thing.
- Storage class specifiers help you to specify the type of storage used for data objects.
Duration
There are two kinds of duration:- automatic
- static
Storage Classes
During the execution of a program, each object exists as a physical location in memory for a certain period, called its lifetime. The lifetime is determined by its storage duration which have corresponding storage class specifiers.There are four kinds of storage duration static, thread, automatic and allocated.
Typically
- static & thread storage objects are stored in the Data Segment.
- Automatic storage objects are stored on the Stack.
- Allocated objects are stored on the Heap.
- Static objects
- Thread Duration
Linkage
There are three kinds of linkage:- no linkage
- All identifiers which are not internal or external. Such as:
- Labels, tags, typedef's
- Function parameters
- internal linkage
- Identifiers with internal linkage represent the same object within the same translation unit / Module.
- use storage class specifier static on function or object outside all function definitions
- external linkage
- Identifiers with external linkage represent the same object throughout the program.
- functions are extern by default.
- use storage class specifier extern on function or object.
- once an identifier has been declared intern, you cannot change it back to extern.
Sources
21nov16 | admin |