Names Bindings and Scope - sammanthp007/Linux-Kernel-Development GitHub Wiki
Variables
A variable can be characterized as a sextuple of attributes: (name, address, value, type, lifetime, and scope).
The address of a variable is sometimes called its l-value, because the address is what is required when the name of a variable appears in the left side of an assignment.
It is possible to have the same variable to be associated with different addresses at different times during the execution of the program. For example, if a subprogram is called from different programs, the same variable is instantiated with different address.
It is possible to have multiple variables that have the same address. When more than one variable name can be used to access the same memory location, the variables are called aliases.
Binding
Bindings can take place at language design time, language implementation time, compile time, load time, link time, or run time.
Binding based on Type:
- Static: 2 Types
- a. Implicit:
- Different namespace: @apple or %apple or $apple
- Type inference: in c#, var a = 2 means a is an integer
- Explicit
- a. Implicit:
- Dynamic
There are two disadvantages to dynamic type binding.
-
First, it causes programs to be less reliable, because the error-detection capability of the compiler is diminished relative to a compiler for a language with static type bindings. Dynamic type binding allows any variable to be assigned a value of any type. 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.
-
Perhaps the greatest disadvantage of dynamic type binding is cost. The cost of implementing dynamic attribute binding is considerable, particularly in execution time. Type checking must be done at run time. Furthermore, every variable must have a run-time descriptor associated with it to maintain the cur- rent type. The storage used for the value of a variable must be of varying size, because different type values require different amounts of storage.
Finally, languages that have dynamic type binding for variables are usually implemented using pure interpreters rather than compilers. Computers do not have instructions whose operand types are not known at compile time. There- fore, a compiler cannot build machine instructions for the expression A + B if the types of A and B are not known at compile time. Pure interpretation typically takes at least 10 times as long as it does to execute equivalent machine code.
In reality, the names of variables are never bound to types. Names can be bound to variables and variables can be bound to types.
Bindings based on storage:
- Static variables
- Stack-dynamic variable
- Implicit-heap binding
- Explicit-heap binding
Stack-dynamic variable
- The advantages of stack-dynamic variables are as follows: To be useful, at least in most cases, recursive subprograms require some form of dynamic local storage so that each active copy of the recursive subprogram has its own ver- sion of the local variables. These needs are conveniently met by stack-dynamic variables. Even in the absence of recursion, having stack-dynamic local storage for subprograms is not without merit, because all subprograms share the same memory space for their locals.
- The disadvantages, relative to static variables, of stack-dynamic variables are the run-time overhead of allocation and deallocation, possibly slower accesses because indirect addressing is required, and the fact that subprograms cannot be history sensitive. The time required to allocate and deallocate stack- dynamic variables is not significant, because all of the stack-dynamic variables that are declared at the beginning of a subprogram are allocated and deallocated together, rather than by separate operations.
- Explicit heap-dynamic variable:
Explicit heap-dynamic variables are nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions written by the programmer.
- Implicit heap-dynamic variable:
The advantage of such variables is that they have the highest degree of flexibility, allowing highly generic code to be written. One disadvantage of implicit heap-dynamic variables is the run-time overhead of maintaining all the dynamic attributes, which could include array subscript types and ranges, among others. Another disadvantage is the loss of some error detection by the compiler.
Referencing environment
The referencing environment of a statement is the collection of all variables that are visible in the statement.
Two cases:
- Static scoping
- Dynamic scoping
Named constant
A named constant is a variable that is bound to a value only once.
The binding of a variable to a value at the time it is bound to storage is called initialization.