Imperative Languages & State
Key Concepts
Mental model is a slightly more abstract view
of the Von Neumann architecture
- all about state and
state changes
- a program is a series of
statements that lead to
a final state (probably including output, so changing the
state of the external environment)
- variables represent units
within the state of the program than can change (vary)
as the program runs
Variables
Variables have 6 attributes, which we can represent as a sextuple:
(Name,
Address,
Value,
Type,
Lifetime,
Scope)
-
Name:
identifies the variable (Names also apply to
functions/procedures, constants, and sometimes user-defined
types, so are more generically called identifiers)
- Rules for valid names are part of the
syntax of a language
- Identifiers should not be mistaken for
keywords
(some languages allow overlap; most do not)
- Rules affect readability, writeability
-
Type:
determines
- size in memory
- range of possible values
- set of valid operations
(What about abstract types, such as Java interfaces?)
-
Lifetime:
when is the memory set aside and when is it "freed"?
(allocation / deallocation)
- Examples: parameters, local variables, global variables,
class variables
- Terms: static, stack-dynamic, explicit heap-dynamic,
implicit heap-dynamic
- Examples: what about
new
AquaFish
, malloc()
?
-
Scope:
where can the variable (or constant or function) be
"seen" or used?
- Examples: parameters, local variables, global variables,
class variables
- Terms: global scope, static scope, dynamic scope,
referencing environments
Aliases: when two variable names access
the same location in memory
(created via pointers, references, C and C++ unions)
Binding
A binding is an association, such as between variable name
and a memory location, a symbol and an operation,
or a function call and the code that will be executed.
- Static vs. dynamic type binding
(What does "strong typing" vs "weak typing" mean?)
(What about type inference?)
- Static variables, stack-dynamic variables, heap-dynamic
variables
- Binding times: language design / implementation, compile time,
load time, link time, run time
What are the advantages and disadvantages of different language design
decisions?
Alyce Brady, Kalamazoo College