类别 全部 - variables - address

作者:Levi George 3 年以前

247

Chapter_5__Names_Bindings_and_Scopes

Named constants are variables bound to a value only once, enhancing readability and reliability in programming. Initialization of these variables depends on whether the storage binding is static or dynamic, with static bindings occurring before runtime and dynamic bindings allowing for expression-based initial values.

Chapter_5__Names_Bindings_and_Scopes

Chapter 5: Names, Bindings, and Scopes

Named Constants

Initialization
If the storage binding is dynamic, initialization is also dynamic and the initial values can be any expression.
If the variable is statically bound to storage, binding and initialization occur before run time.
The binding of a variable to a value at the time it is bound to storage.
Parameterize a program.
Useful as aids to readability and program reliability.
A variable that is bound to a value only once.

5.4: The Concept of Binding

Storage Bindings and Lifetime
Implicit Heap-Dynamic Variables

Run-time overhead of maintaining all the dynamic attributes.

Have the highest degree of flexibility.

Bound to heap storage only when they are assigned values.

Stack-Dynamic Variables

The run-time overhead of allocation and deallocation.

Dynamic local storage so that each active copy of the recursive subprogram has its own version of the local variables.

Allocated from the runtime stack.

whose storage bindings are created when their declaration statements are elaborated, but whose types are statically bound.

Explicit Heap-Dynamic Variables

Difficult to use pointer and reference variables correctly.

Used to construct dynamic structures.

Nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions written by the programmer.

Static Variables

Storage cannot be shared among variables.

Cannot support recursive subprograms.

All addressing of static variables can be direct (faster).

Bound to a memory cell before program execution begins and remains bound to that same memory cell until program execution terminates.

The lifetime of a variable

The time during which the variable is bound to a specific memory location.

Deallocation

The process of placing a memory cell that has been unbound from a variable back into the pool of available memory.

Allocation

The memory cell to which a variable is bound somehow must be taken from a pool of available memory.

Binding of Attributes to Variables
Type Bindings

Types can be specified statically through some form of explicit or implicit declaration.

Before a variable can be referenced in a program, it must be bound to a data type.

When the binding takes place.

How the type is specified.

Dynamic

Implemented using pure interpreters rather than compilers.

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.

Causes programs to be less reliable.

Provides more programming flexibility.

Name can be thought of as being only temporarily bound to a type.

The variable is bound to a type when it is assigned a value in an assignment statement.

The type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name.

If the binding first occurs during run time or can change in the course of program execution.

Static

explicit declaration

Some of the problems with implicit declarations can be avoided by requiring names for specific types to begin with particular special characters.

Prevent the compilation process from detecting some typographical and programmer errors.

Implicit variable type binding is done by the language processor, either a compiler or an interpreter.

a means of associating variables with types through default conventions, rather than declaration statements.

a statement in a program that lists variable names and specifies that they are a particular type.

If it first occurs before run time begins and remains unchanged throughout program execution.

Binding
can take place at language design time, language implementation time, compile time, load time, link time, or run time.
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.

5.6: Scope and Lifetime of Variables

Both of these concepts become fuzzy with subprogram calls and in a number of other situations outside of function calls from main.
C++'s 'static' keyword extends lifetime beyond the life of the function, but doesn't alter scope.
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
Often restricted to the function range
Scope is the "reach" that a variable has, where it may be accessed.

5.3: Variables int varName = varValue

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)
Address
Addresses are the computer memory cells that store information in them regarding the variable. This is not seen directly by programmers.
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

Value
That which is stored within a variables memory. placed on the RHS of a declaration or assignment.