Chapter 5: Names, Bindings, and Scopes
5.3: Variables
int varName = varValue
Value
That which is stored within a variables memory. placed on the RHS of a declaration or assignment.
Name
Names are the way that we identify variables. Immediately after type (varName in this case)
Aliases
One address can be referenced by two names, not good for
reading code
Address
Addresses are the computer memory cells that store information in them regarding the variable. This is not seen directly by programmers.
Type
The type of information stored, this in part determines the amount of memory and how that information is stored. A long int will take up more space than a short or regular int. (The type in this case is int, it precedes the variable name. In some cases it is not needed)
5.6: Scope and Lifetime of Variables
Scope is the "reach" that a variable has, where it may be accessed.
Often restricted to the function range
Scope is where the function can be reached,
Lifetime is temporal and is the time that the memory for that variable exists and is allocated
C++'s 'static' keyword extends lifetime beyond the life of the function, but doesn't alter scope.
Both of these concepts become fuzzy with subprogram calls and in a number of other situations outside of function calls from main.
5.4: The Concept of Binding
Binding
An association between an attribute and an entity, such as
between a variable and its type or value, or between an operation and a symbol.
Binding Time
The time at which a binding takes place.
can take place at language design time,
language implementation time, compile time, load time, link time, or run time.
Binding of Attributes to
Variables
Static
If it first occurs before run time begins and remains
unchanged throughout program execution.
explicit declaration
a statement in a program that lists variable names
and specifies that they are a particular type.
a means of associating variables with types through default conventions, rather than declaration statements.
Implicit variable type binding is done by the language processor, either a compiler or an interpreter.
Prevent the compilation process from detecting some typographical and programmer errors.
Some of the problems with implicit declarations can be avoided by requiring names for specific types to begin with particular special characters.
Dynamic
If the binding first occurs during run time or can change in the course of program execution.
The type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name.
The variable is bound to a type when it is assigned a value in an assignment statement.
Name can be thought of as being only temporarily
bound to a type.
Provides more programming flexibility.
Causes programs to be less reliable.
Incorrect types of right sides of assignments are not detected as errors; rather, the type of the left side is simply changed to the incorrect type.
Implemented using pure interpreters rather than compilers.
Type Bindings
Before a variable can be referenced in a program, it must be bound to a data type.
How the type is specified.
When the binding takes place.
Types can be specified statically through
some form of explicit or implicit declaration.
Storage Bindings and
Lifetime
Allocation
The memory cell to which a variable is bound somehow must be taken from a pool of available memory.
Deallocation
The process of placing a memory cell that has been unbound from a variable back into the pool of available memory.
The lifetime of a variable
The time during which the variable is bound to a
specific memory location.
Static Variables
Bound to a memory cell before program execution begins and remains bound to that same memory cell until program execution terminates.
All addressing of static variables can be direct (faster).
Cannot support recursive subprograms.
Storage cannot be shared among variables.
Explicit Heap-Dynamic
Variables
Nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions written by the programmer.
Used to construct dynamic
structures.
Difficult to use pointer and reference variables correctly.
Stack-Dynamic Variables
whose storage bindings are created when their declaration statements are elaborated, but whose types are statically bound.
Allocated from the runtime stack.
Dynamic local storage so that each active copy of the recursive subprogram has its own version of the local variables.
The run-time overhead of allocation and deallocation.
Implicit Heap-Dynamic
Variables
Bound to heap storage only when they
are assigned values.
Have the highest degree of
flexibility.
Run-time overhead of maintaining all
the dynamic attributes.
5.1: Introduction
Variables are representations of machine memory
Defined by numerous properties
variables are noted by name
stored based on type
used based on scope and lifetime
5.2 Names
Name and identifier are interchangeable terms that are strings of characters used to identify an entity in a program. This could be a variable, a subprogram, formal parameter, etc.
Design Issues
Should they be case sensitive?
Case sensitivity results in less readable code!
Are special words of the language reserved words or keywords?
A reserved word is a special word of a programming language that cannot be used as a name. A programmer cannot reassign a reserved word.
Keywords are special words of a programming language that can be reassigned by a programmer.
Names can be of different lengths, and some languages set a specific length that a name can be. Other languages have an infinite length, but is only recognized by the first X characters. Finally, some languages allow infinite length and are recognized by that infinite length.
5.7 Referencing Environments
The referencing environment of a statement is the collection of all variables that are visible in the statement.
Statically scoped languages
In a statically scoped language, the referencing environment is the variables declared in its local scope plus the collection of all variables of its ancestor scopes that are visible.
Dynamically scoped languages
In a dynamically scoped language, the referencing environment is the locally declared variables plus the variables of all other active subprograms.
An active subprogram is one whose execution has begun, but not yet terminated.
Named Constants
A variable that is bound to a value only once.
Useful as aids to readability and program reliability.
Parameterize a program.
Initialization
The binding of a variable to a value at the time it is bound to
storage.
If the variable is statically bound to storage, binding and initialization occur before run time.
If the storage binding is dynamic, initialization is also dynamic and the initial values can be any expression.
5.5: Scope
Static Scope
prior to execution (static) scope is set, tell type by reading code
nested scopes vs. non nested scopes
static parent - the declarator of a subprogram
static ancestor - the declarator of a static parent
Blocks
Originated ALGOL 60
minimized scope with dedicated local variables
variables are stack dynamic, allocated when code is entered, deallocated when code is exited.
Declaration Order
Variables typically must be declared before use
Different languages have different requirements
C++ allow declarations in for loops
JS, allows declaration at bottom of block or function
Global Scope
Global variables reach entire program (very bad)
declaration vs. definition
Some variables simply need to be at top of file, others need keyword.
extern in C
You can use scope operator (::) to access global "hidden" by local
Eval of Static Scoping
allows access to variables and subprograms, moreso than necessary
Due to variable nature of development, causes issues when restructuring programs. -- Solution? Encapsulation constructs
Dynamic Scope
Dynamic - scope can only be determined at runtime
searches local declarations, then parents, then ancestors, if no declaration is found, run-time error.
Eval of Dynamic
can't read and determine scope
Issue 1: low reliability, local vars of subprograms are visible to other running subprograms.
Issue 2: hard to type check references to non-local variables, harder to read and understand code.
Lastly: dynamic scoping requires long times to access appropriate nonlocals.
Although, dynamic scoping allows bypassing of parameterizing for functions. Static - reliable, easy to read, fast
dynamic - slow, easy to access, unreliable at times