CS 3733 Operating Systems Notes: Storage and Linkage Classes
This material is from Chapter 2 of PUP.
Storage classes: static and automatic
- static storage class refers to variables that, once allocated, persist
throughout the execution of a program.
-
automatic storage class refers to variables which come into existence when the
block in which they are declared is enteredand are discarded when the defining
block is exited.
- Variables declared inside a function have automatic storage class unless
they are declared to be static.
These are usually allocated on the program stack.
-
Variables defined outside any funtions have static storage class.
The word static has two meanings is C.
One is related to storage class and the other to linkage class.
Linkage class determines whether variables can be accessed in files other than
the one in which they are declared.
- Internal linkage class means they can only be accessed in the file in which
they are declared.
- External linkage class means they can be accessed in other files.
- variables decalred outside any function and function name identifiers have
external linkage by default.
They can be given internal linkage with the key word static.
-
Varibales declared inside a function are only known inside that function and
are said to have no linkage.
Object |
Where Declared |
static Modifies |
static Applied? |
Storage Class |
Linkage Class |
variable | inside a function | storage class | yes | static | none |
variable |
inside a function |
storage class |
no |
automatic |
none |
variable |
outside any function |
linkage class |
yes |
static |
internal |
variable |
outside any function |
linkage class |
no |
static |
external |
function |
outside any function |
linkage class |
yes |
  |
internal |
function |
outside any function |
linkage class |
no |
  |
external |
Table 2.1 (page 31, modified):
Effect of using the static keyword modifier in a C program.
Example: Assume that the following are in a file called bubblesort.c
Give the storage class and linkage class of each of the
variables and functions in the following program.
static int count = 0;
static int onepass(int a[], int n) { /* return true if interchanges are made */
int i;
int interchanges = 0;
int temp;
for (i = 0; i < n - 1; i++)
if (a[i] > a[i+1]) {
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
interchanges = 1;
count++;
}
return interchanges;
}
void clearcount( ) {
count = 0;
}
int getcount( ) {
return count;
}
void bubblesort(int a[], int n) { /* sort a in ascending order */
int i;
for (i = 0; i < n - 1; i++)
if (!onepass(a, n - i))
break;
}
- The function onepass has internal linkage.
- The other functions have external linkage.
- Functions do not have a storage class.
- The count variable has internal linkage and static storage.
- All other variables have no linkage and automatic storage.