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.

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.