Microsoft PowerPoint – 16_C_Storage_Class_Scope_Linkage
O
SU
C
SE
2
42
1
J.E.Jones
Required Reading: Pointers On C, Chapter 3, Sections 3.5 through 3.10 with a special
interest in Table 3.4.
Use https://www.tutorialspoint.com/cprogramming/c_storage_classes.htm as an
additional resource
O
SU
C
SE
2
42
1
J. E. Jones
When programming in any language, it is usually
important to know what type of memory holds your
data
◦ Stack
◦ Heap
◦ Disk
◦ Read-Only Memory, etc.
It’s also useful to know when (e.g.. within what
functions, for example) variable identifiers are
available to access that data
O
SU
C
SE
2
42
1
J. E. Jones
A Process can be defined as a group of functions that work together to perform a
particular job. (e.g., main(), other functions written by the software developer and C
library functions. Executable on disk/Process loaded in main memory ready for CPU
Stack
1. A place in memory where information specific to a particular function is stored
each time it is invoked.
2. It is allocated when the function is called and deallocated when the function
returns.
3. When a function stops executing or returns, any variable declared and stored on the
stack is no longer available.
4. Essential for recursive function calls (consider function parameters).
Heap
1. A place in memory where information specific to a particular process is stored
each time it is invoked.
2. Any values stored here are available until the process stops executing.
3. Where calloc() and malloc() memory addresses are.
4. Essential for values that must be persistent within a process no matter what
function is currently executing.
O
SU
C
SE
2
42
1
J. E. Jones
1. Storage class: determines (a) the type of storage and
the lifetime of the storage associated with a variable, and
(b) the variable’s initialization (if any).
2. Storage Scope: determines the region of a program in
which a variable can be accessed directly (i.e., by name,
and not through a pointer).
3. Linkage: for a given name (identifier), linkage
determines whether the same name in a different file
refers to the same variable or function, or some different
variable or function [e.g., You have someone in 3 of your
classes named Noah. Is it the same person or 3 different
people all named Noah?]
O
SU
C
SE
2
42
1
J. E. Jones
Storage class: 4 types (only 3 really)
◦ static
◦ automatic (or auto)
◦ register
◦ external (not really storage class, but linkage)
◦ THIS CLASS HAS NO RELATIONSHIP WITH CLASSES IN C++
Storage Scope: 4 types (we will only cover 3)
◦ block
◦ file
◦ prototype
◦ function (very limited use (only labels for goto); we will not cover this)
Storage Linkage: 3 types
◦ internal
◦ external
◦ none (no linkage)
CAUTION: Learn the terms! “Global” and “Local” are not correct terms for storage
class, scope, or linkage in C! [You will lose points if you use these on an exam.]
O
SU
C
SE
2
42
1
J. E. Jones
Identifiers that are used as variable names can
have class, scope, and linkage
Identifiers that are used as function names can
only have scope and linkage.
◦ Functions aren’t stored on the stack or the heap.
◦ If we are asked what the storage class for a function name
identifier is, then it’s undefined.
O
SU
C
SE
2
42
1
J. E. Jones
• Refers to the type of memory in which the variable’s value is stored, which in
turn defines different characteristics for the variable
◦ static (keyword) – The heap
◦ automatic* (keyword) – The runtime stack
◦ register (keyword) – Hardware registers
• Determines when the variable is created and destroyed and how long it will
retain its value. Note this information on the following slides.
• Determines whether the variable will be initialized by the compiler (the
loader actually) or whether it must be done explicitly by the program code
• The default storage class for a variable depends on where it is declared (see
following slides for different possible places in a C program)
• Function identifiers do not have a storage class.
• They are not stored on the heap or the stack
*automatic keyword is not required
O
SU
C
SE
2
42
1
J. E. Jones
• Declared outside any/all blocks
◦ Storage class is static (stored on the heap)
◦ No way to specify any other
storage class for these variables
◦ These variables are created before the
program begins to run (or dynamically
at runtime – more later) and exist throughout its entire program execution.
◦ These variables are initialized by default (by the loader) to a value of 0
appropriate to their declared type
◦ You can initialize these variables to some other initial value in the
declaration statement
◦ They retain last value they were assigned until the program completes
(i.e., the values are retained from the completion of one function to the
beginning of the next function)
◦ Can be declared above all functions in the program, but can also be declared
between two functions (this affects scope, though; see below)
#include
int section;
int main() {
section=1;
…….
}
O
SU
C
SE
2
42
1
J. E. Jones
• Declared within a block
◦ Storage class default is automatic (stored
on the runtime stack)
◦ Created just before execution of any code in the block in which they are
declared
◦ “Discarded” (technically, we should say “inaccessible”) just as execution
leaves that block
◦ These variables must be explicitly initialized by your code, or they will
contain garbage (unknown/unpredictable values)
◦ New copies created each time the block is executed, so values are not
retained in between each of multiple executions of the code in the block
(for example, between function calls).
◦ Parameters passed to functions have automatic storage class also (to
support recursion), and there is no way to change this (see below)
#include
int func1(char num1) {
int var1;
…….
}
O
SU
C
SE
2
42
1
J. E. Jones
• Variables declared within a block but with the keyword static
change storage class from automatic (the default) to static
• This changes where the variable is stored from the stack to the heap
• Static storage class exists for the entire duration of the
program, rather than just the duration of the block in which
the variable is declared
• NOTE: changing of the storage class of a variable does not
change its scope (see below); it is still accessible by name
only from within the block
• NOTE: parameters to a function cannot be declared static,
because arguments are always passed on the stack to support
recursion. Thus, the keyword static cannot be used with
parameters.
O
SU
C
SE
2
42
1
J. E. Jones
• No. When used in declarations of variables that appear outside of blocks,
or in function definitions (i.e., the function is designated as static)
◦ The keyword static changes the linkage of the identifier from
external to internal
◦ The storage class and scope are not affected.
◦ Thus, this is really a distinct use of the keyword static in C, because
it relates to linkage, and not to storage class (although historically, it
was referred to as storage class)!
◦ Accessible only from within the source file in which they were
declared
◦ This is used to limit the accessibility of “global” variables (variables
declared outside any block) in library functions (so that, if you
happen to use a global variable in your program with the same name
as a global in the library function, you will not get unexpected
results).
O
SU
C
SE
2
42
1
J. E. Jones
• So,
• When the keyword static is used within a block, it
changes a variable’s class
• When the keyword static is used outside of any/all
blocks is changes an identifier’s linkage
O
SU
C
SE
2
42
1
J. E. Jones
• Variables within a block can be declared with the keyword
register
• Can be used on automatic variables only to indicate that they
should be stored in the machine’s hardware registers rather
than in memory (on the stack).
• WHY? To be accessed faster (~100x faster)
• NOTE – the compiler can ignore this if necessary; i.e., if too
many variables designated as register (first come, first
served) rest are automatic
• Typically declare heavily used variables as register variables
(i.e., loop indices)
• Created and destroyed at the same time as automatic
variables
O
SU
C
SE
2
42
1
J. E. Jones
• Automatic
◦ Default storage class for variables declared inside blocks
◦ Discarded on exit from block
◦ Can have auto specifier – but it is redundant (it’s the default)
◦ Can have register specifier
• Stored in fast registers of the machine, if possible, instead of
RAM
• Means variable will be stored on the stack
• Static
◦ Default storage class for variables declared outside of any block
• Declared outside function blocks
◦ Can also be declared within a function with keyword static
• Initialized at load time and retains its value between calls to
function
• Initial value must be a constant known at compile time
• Means variable will be stored on the heap
O
SU
C
SE
2
42
1
J. E. Jones
The area in the program in which an identifier may be used to access a variable directly
(i.e., not through a pointer)
◦ We’ve already briefly discussed variables with file scope
For example, the scope of the variables declared inside a block in a function is limited to
that block in that function
This means:
◦ No other function may access these variables by their identifier names because the
names cannot validly refer to these variables outside of their scope
◦ It is legal to declare different variables with the same names as long as they are not
declared in the same scope.
Scope types:
◦ block
◦ file
◦ prototype
◦ function (only relevant to labels with goto – we will not cover this)
Where an identifier is declared determines its scope
O
SU
C
SE
2
42
1
J. E. Jones
• A block is a list of statements enclosed in braces
• Any identifiers declared at the beginning of the block (where
they must be in ANSI C89/C90) are accessible to all
statements in the block
• The formal parameters of a function definition also have
block scope in the function’s body
◦ Local variables declared in the outermost block cannot have the same
name as any of the parameters because they are all declared in the
same scope
• Nested blocks having declarations of variables with the same
name
◦ The outer block variable cannot be referenced by name from within
the inner block, because they are hidden by the variable declared in
the inner block (DO NOT DO THIS, because it decreases program
readability, and is error-prone)
O
SU
C
SE
2
42
1
J. E. Jones
• Any identifier declared outside of all blocks
• Means that the identifier may be accessed
anywhere from the end of its declaration to
the end of the source file in which it was
declared
• We talked about file scope in slide deck 3
O
SU
C
SE
2
42
1
J. E. Jones
• Applies to any variable (parameter) names given in a function prototype
(declaration)
• It does not apply to the function name in a prototype statement, only the parameters
specified.
• Remember it’s not required to supply names (only types are required)
• Any variable with Prototype Scope has no memory class or linkage
(static/automatic/register) because no memory has been allocated for
the variable
• Prototype scope extends uniquely to each prototype (the prototype scope of
one prototype is different from that of every other prototype)
Examples:
1. void function1(int var1, int var2);
var1 and var2 have prototype scope, no memory is allocated for these
variables at this time, function1 has file scope
2. void function1(int var1, int var2){code}
var1 and var2 have block scope/auto class, function1 has file scope
3. void function2(int var3, int var4);
var3 and var4 have prototype scope, no memory is allocated for these
variables at this time, function2 has file scope
O
SU
C
SE
2
42
1
J. E. Jones
• Linkage determines how multiple occurrences of a file scope
identifier with the same name (in different files that we plan
to use together) are treated
• The scope of an identifier is related to its linkage, but the two
properties are not the same:
• scope is the part of a program in each source file where an identifier
can be accessed by its name, but
• linkage relates to whether occurrences of the same identifier in two
different source files refer to the same thing, or they are unique
identifiers. For that reason, linkage (other than none) only relates to
file scope variables and functions.
O
SU
C
SE
2
42
1
J. E. Jones
• 3 types of linkage
◦ None (no linkage)
• Identifiers that have no linkage (variables without file scope) are
always individuals; i.e., multiple declarations using the same
identifier are always treated as separate and distinct entities. All
block scope variables(none) and prototype scope variables
(undefined).
◦ Internal linkage
• All references to the file scope identifier within one source file
refer to a single variable (the one declared in that source file), but
declarations of, and references to, a duplicate identifier in other
source files refer to different variable memory location
◦ External linkage
• All references to a file scope identifier in any source file refer to
the same entity
• Variable known to all functions in the program; declared outside
any function.
O
SU
C
SE
2
42
1
J. E. Jones
• After the individual source files comprising a
program are compiled, the object files are
linked together with functions from one or
more libraries to form the executable
program
O
SU
C
SE
2
42
1
J. E. Jones
Default linkage of a file scope variable is external.
Can use the keyword extern with the variable name
that is declared with identical name in other files.
◦ Not required, but
1. it allows you to choose which of the .c file’s variable will be
used to allocate space, and
2. increases readability/understanding of the overall program.
Allows a new reader of the program (or you 6 months from
now) know that this variable is recognized as having external
linkage and it was done purposefully.
O
SU
C
SE
2
42
1
J. E. Jones
int season;
void function1(){
}
extern int season;
int function2(){
season=3;
}
File1.c File2.c
Allocate space here and can use
season as a file scope variable
in any program within this file
Can use season as a file scope
variable in any program within
this file, but it’s allocated space
in File1.c (not in File2.c).
O
SU
C
SE
2
42
1
J. E. Jones
We can change an external file scope variable’s linkage
to internal, use the keyword static.
◦ e.g. int function_level;
static int function_level;
Internal linkage means that no programs except those in
the .c file with the internal linkage declaration can
access the variable declared.
External linkage
Internal linkage
O
SU
C
SE
2
42
1
J. E. Jones
int season;
long function1(){
}
File1.c File2.c
extern int season;
void function2(){
}
static int season;
int function3(){
}
File3.c
These two files use same variable called
season that is allocated space in File1.c
This is external linkage. Must have
identical data type.
This file uses a different variable
called season that is only referenced
in this file. File3.c code can’t
reference the variable season
declared in File1.c. This is internal
linkage. Can be a different data type
than in File1.c/File2.c
O
SU
C
SE
2
42
1
J. E. Jones
Variables outside of blocks: default class: static/can’t be changed, file scope,
default linkage: external/can be changed to internal with
keyword static
Prototype statements: Function Name has file scope, External linkage can change to internal
w/static keyword, undefined classs
parameters have undefined class; have prototype scope, can’t change,
linkage: undefined
Function_Name(parameters): Function Name has file scope, External Linkage; can change
to internal w/static keyword, undefined class
parameters: default class: automatic/can’t be changed
default scope: block/can’t be changed
default linkage: none/can’t be changed
{
variables inside block: default class: automatic/can be changed to static or register with
keyword
default scope: block/can’t be changed,
default linkage: none/can’t be changed
}
O
SU
C
SE
2
42
1
J. E. Jones
Assignment: Class/Scope/Linkage due tomorrow at
noon.