Storage Class, Scope, and Linkage
What is a process?
a group of functions that work together to perform a particular job
What is external linkage?
all references to a file scope identifier in any source file refer to the same entity
What is internal linkage?
all references to the file scope identifier within 1 source file refer to a single variable, but declarations/references to a duplicate identifier in other source files refer to different variable memory locations
What kind of variable does prototype scope apply to?
any variable (parameter) names given in a function prototype (declaration)
What kind of storage class do parameters passed to functions have?
automatic storage class
What is the scope of a variable declared inside a block in a function?
block
What happens when the keyword static is used within a block?
changes a variable's class
What happens when the keyword static is used outside of any/all blocks?
changes a variable's linkage
What happens when you use the keyword static in declarations of variables that appear outside of blocks/in function definitions?
changes the linkage of the identifier from external to internal
What is the class, scope, and linkage of parameters, and can they be changed?
class: auto scope: block linkage: none
What is the class, scope, and linkage of variables inside blocks, and can they be changed?
class: automatic (change to static with keyword static or register with keyword register) scope: block linkage: none
What is the class, scope, and linkage of variables outside of blocks, and can they be changed?
class: static scope: file scope linkage: external (change to internal with keyword static)
What is the class, scope, and linkage of function names, and can they be changed?
class: undefined scope: file linkage: external (change to internal with keyword static)
What is the class, scope, and linkage of prototype statements, and can they be changed?
class: undefined scope: prototype linkage: undefined
What is linkage?
determines for a given name whether the same name in a different file refers to the same variable or function
What is storage scope?
determines the region of a program in which a variable can be accessed directly
What is storage class?
determines the type and lifetime of the storage and the variable's initialization
How long does static storage class exist during the program?
entire duration of the program
What is the default linkage?
external
T/F: Function identifiers have a storage class.
false; do not have a storage class
T/F: Changing of the storage class of a variable changes its scope.
false; doesn't change its scope
T/F: It is never legal to declare different variables with the same names.
false; it is legal as long as they're not declared in the same scope
T/F: The default storage class for a variable depends on when it is declared.
false; where it is declared
What type of scope does function1, var1, and var2 have in the following lines of code? void function1(int var1, int var2) { code }
function1: file scope var1: block scope var2: block scope
What type of scope does function1, var1, and var2 have in the following line of code? void function1(int var1, int var2);
function1: file scope var1: prototype scope var2: prototype scope
What type of scope does function2, var3, and var4 have in the following line of code? void function1(int var1, int var2); void function2(int var3, int var4);
function2: file scope var3: prototype scope var4: prototype scope
What is file scope?
identifier may be accessed anywhere from the end of its declaration to the end of the source file in which it is declared
When are automatic storage classes discarded?
just as execution leaves that block
When are automatic storage classes created?
just before execution of any code in the block in which they're declared
What is a block?
list of statements enclosed in braces
What are identifiers that have no linkage?
multiple declarations using the same identifier are always treated as separate and distinct entities
Can we only use static on block scope variables?
no
Does using the keyword static in declarations of variables that appear outside of blocks/in function definitions affect the storage class and scope?
no
How does a variable with prototype scope affect storage class and linkage?
no storage class or linkage
Can the keyword static be used with parameters to a function?
no; arguments are always passed on the stack to support recursion
What happens after the individual sources files are compiled?
object files are linked together with functions from 1 or more libraries to form the executable program
Where are static storage classes declared?
outside any/all blocks
Where are file scope variables declared?
outside of all blocks
What is scope?
part of a program in each source file where an identifier can be accessed by its name
What is linkage?
relates to whether occurrences of the same identifier in 2 different source files refer to the same thing or are unique identifiers
What are 3 keywords for storage class?
static (meaning the heap) automatic (meaning the runtime stack) register (meaning the hardware registers)
Why would you use the keyword register?
to access a variable faster
T/F: Any identifiers declared at the beginning of the block are accessible to all statements in the block.
true
T/F: New copies of values are created each time the block is executed, so values aren't retained in between multiple executions of code in the block.
true
T/F: Prototype scope extends uniquely to each prototype.
true
T/F: Static storage class variables retain the last value they were assigned until the program completes.
true
T/F: The formal parameters of a function definition have block scope in the function's body.
true
T/F: Variables designated as register are created and destroyed at the same time as automatic variables (just before execution of any code in the block in which they're declared/just as execution leaves that block).
true
How can you use internal linkage?
use static keyword
How can you change a variable declared within a block from automatic to static?
use the keyword static
When should variables within a block be declared with the keyword register?
used on automatic variables to indicate that they should be stored in the machine's hardware registers rather than in memory (on the stack)
What are static storage class variables initialized to by default?
value of 0 appropriate to their declared type
What are 2 things storage class determine?
when the variable is created and destroyed/how long it will retain its value whether the variable will be initialized by the compiler or whether it must be done explicitly by the program code
What determines an identifier's scope?
where it is declared
What happens if you don't explicitly initialize automatic storage classes?
will contain garbage
Where are automatic storage classes declared?
within a block
Can you initialize static storage class variables to some other initial value in the declaration statement?
yes
Can static storage class variables be declared between 2 functions?
yes; affects scope
What are 2 things that happen if you use the keyword extern?
1. allows you to choose which of the .c file's variable will be used to allocate space 2. increases readability/understanding of the overall program
What are the 4 types of storage scope?
1. block 2. file 3. prototype 4. function
What are the 3 types of storage linkage?
1. internal 2. external 3. none
What are the 4 characteristics of stack?
1. place in memory where information specific to a particular function is stored each time it's invoked 2. allocated when the function is called and deallocated when the function returns 3. any variable declared and stored on the stack is no longer available when a function stops executing or returns 4. essential for recursive function calls
What are the 4 characteristics of heap?
1. place in memory where information specific to a particular process is stores each time it's invoked 2. any values stored 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
What are the 4 types of storage class?
1. static 2. automatic 3. register 4. external
