OSU CSE 2421
Required Reading: Pointers On C, Chapter 3, Sections 3.5 through 3.10 with a special interest in Table 3.4.
J.E.Jones
OSU CSE 2421
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
J. E. Jones
OSU CSE 2421
Storage class: 4 types (only 3 really) ◦ static
◦ automatic(orauto)
◦ register
◦ external(notreallystorageclass,butlinkage)
◦ THIS CLASS HAS NO RELATIONSHIP WITH CLASSES IN C++
Storage Scope: 4 types (we will only cover 3) ◦ block
◦ file
◦ prototype
◦ function(verylimiteduse(onlylabelsforgoto);wewillnotcoverthis)
Storage Linkage: 3 types ◦ internal
◦ external
◦ none(nolinkage)
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.]
J. E. Jones
OSU CSE 2421
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?]
J. E. Jones
OSU CSE 2421
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.
J. E. Jones
OSU CSE 2421
• 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
J. E. Jones
OSU CSE 2421
• Declared outside any/all blocks
◦ Storage class is static (stored on the heap) ◦ No way to specify any other
#include
storage class for these variables
◦ These variables are created before the
int main() { section=1; …….
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)
}
J. E. Jones
OSU CSE 2421
• Declared within a block
◦ Storage class default is automatic (stored
#include
int func1(char num1) {
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)
}
int var1; …….
J. E. Jones
OSU CSE 2421
• Variables declared within a block but with the keyword static change storage class from automatic (the default) to static
• Thischangeswherethevariableisstoredfromthestacktotheheap
• 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.
J. E. Jones
OSU CSE 2421
• 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).
J. E. Jones
OSU CSE 2421
• 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
J. E. Jones
OSU CSE 2421
• 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
J. E. Jones
OSU CSE 2421
• 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
•
Means variable will be stored on the stack
• Stored in fast registers of the machine, if possible, instead of RAM
• 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
•
Means variable will be stored on the heap
• •
Initialized at load time and retains its value between calls to function
Initial value must be a constant known at compile time
J. E. Jones
OSU CSE 2421
#include
int function1(); int main() {
}
int function2(int q); int function1(){
}
int function2(int q){
}
register int i;
int limit=16;
section=1;
function1();
for (i=0; i
int a;
if (section == 1) then section =4 else section=3;
J. E. Jones
OSU CSE 2421
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:
◦ Nootherfunctionmayaccessthesevariablesbytheiridentifiernamesbecausethe
names cannot validly refer to these variables outside of their scope
◦ Itislegaltodeclaredifferentvariableswiththesamenamesaslongastheyarenot declared in the same scope.
Scope types: ◦ block
◦ file
◦ prototype
◦ function(onlyrelevanttolabelswithgoto–wewillnotcoverthis)
Where an identifier is declared determines its scope
J. E. Jones
OSU CSE 2421
• 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)
J. E. Jones
OSU CSE 2421
• Anyidentifierdeclaredoutsideofallblocks
• Meansthattheidentifiermaybeaccessed anywhere from the end of its declaration to the end of the source file in which it was declared
• Wetalkedaboutfilescopeinslidedeck3
J. E. Jones
OSU CSE 2421
• 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
J. E. Jones
OSU CSE 2421
• 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:
• scopeisthepartofaprogramineachsourcefilewhereanidentifier can be accessed by its name, but
• linkagerelatestowhetheroccurrencesofthesameidentifierintwo 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.
J. E. Jones
OSU CSE 2421
• 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.
J. E. Jones
OSU CSE 2421
• Aftertheindividualsourcefilescomprisinga program are compiled, the object files are linked together with functions from one or more libraries to form the executable program
J. E. Jones
OSU CSE 2421
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.
J. E. Jones
OSU CSE 2421
File1.c
File2.c
int season;
void function1(){
extern int season; int function2(){
season=3; }}
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).
J. E. Jones
OSU CSE 2421
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;
External linkage
Internal linkage means that no programs except those in the .c file with the internal linkage declaration can access the variable declared.
Internal linkage
J. E. Jones
OSU CSE 2421
File1.c
File2.c
File3.c
int season;
long function1(){
extern int season; void function2(){
static int season; int function3(){
}}}
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
J. E. Jones
OSU CSE 2421
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
{
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
default scope: block/can’t be changed
default linkage: none/can’t be changed
J. E. Jones