1/80
Looks like no tags are added yet.
What is a variable?
An abstraction of a (computer) memory cell; characterized by six attributes: name, address, value, type, lifetime, scope
What are the six attributes (sextuple) of a variable?
Name, address, value, type, lifetime, scope
What are the two design issues for names?
Are names case sensitive? Are special words reserved words or keywords?
Name length limits in C99
No limit, but only the first 63 characters are significant; external names limited to a maximum of 31
Name length in C# and Java
No limit, and all characters are significant
Name length in C++
No limit, but implementers often impose one
Special characters in names: PHP
All variable names must begin with dollar signs
Special characters in names: Perl
All variable names begin with special characters, which specify the variable's type
Special characters in names: Ruby
Names beginning with @ are instance variables; those beginning with @@ are class variables
Disadvantage of case sensitivity in names
Readability: names that look alike are actually different; worse in C++, Java and C# because predefined names are mixed case (e.g., IndexOutOfBoundsException)
Which languages have case-sensitive names?
The C-based languages (C, C++, Java, C#)
Keyword (definition)
A word that is special only in certain contexts
Reserved word (definition)
A special word that cannot be used as a user-defined name
Problem with too many reserved words
Many collisions occur with user-defined names (e.g., COBOL has 300 reserved words)
What are aliases?
Two variable names that can be used to access the same memory location; created via pointers, reference variables, and C and C++ unions; harmful to readability
Address (variable attribute)
The memory address with which the variable is associated; a variable may have different addresses at different times or in different places
Type (variable attribute)
Determines the range of values of variables and the set of operations defined for values of that type; for floating point it also determines precision
Value (variable attribute)
The contents of the location with which the variable is associated
l-value of a variable
Its address
r-value of a variable
Its value
Abstract memory cell
The physical cell or collection of cells associated with a variable
Binding (definition)
An association between an entity and an attribute, 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
Static binding
A binding that first occurs before run time and remains unchanged throughout program execution
Dynamic binding
A binding that first occurs during execution or can change during execution of the program
Language design time binding
Binding operator symbols to operations
Language implementation time binding
Binding a floating point type to a representation
Compile time binding
Binding a variable to a type in C or Java
Load time binding
Binding a C or C++ static variable to a memory cell
Runtime binding
Binding a nonstatic local variable to a memory cell
Two key questions of type binding
How is a type specified, and when does the binding take place? If static, the type may be specified by an explicit or implicit declaration
Explicit declaration
A program statement used for declaring the types of variables
Implicit declaration
A default mechanism for specifying types of variables through default conventions, rather than declaration statements
Languages that provide implicit declarations
Basic, Perl, Ruby, JavaScript, and PHP
Advantage and disadvantage of implicit declarations
Advantage: writability (a minor convenience); Disadvantage: reliability (less trouble with Perl)
Type inferencing
Determining types of variables from context, e.g., C# var with an initial value sets the type; used by Visual Basic 9.0+, ML, Haskell, and F#
Languages with dynamic type binding
JavaScript, Python, Ruby, PHP, and C# (limited)
How is type specified in dynamic type binding?
Through an assignment statement, e.g., JavaScript: list = [2, 4.33, 6, 8]; list = 17.3;
Advantage of dynamic type binding
Flexibility (generic program units)
Disadvantages of dynamic type binding
High cost (dynamic type checking and interpretation) and type error detection by the compiler is difficult
Allocation and deallocation (storage bindings)
Allocation: getting a cell from a pool of available cells; deallocation: putting a cell back into the pool
Lifetime of a variable
The time during which it is bound to a particular memory cell
Static variables (lifetime category)
Bound to memory cells before execution begins and remain bound to the same cell throughout execution, e.g., C and C++ static variables in functions; Advantage: efficiency (direct addressing) and history-sensitive subprogram support; Disadvantage: lack of flexibility (no recursion)
Stack-dynamic variables (lifetime category)
Storage bindings are created when their declaration statements are elaborated; if scalar, all attributes except address are statically bound, e.g., local variables in C subprograms (not declared static) and Java methods; Advantage: allows recursion and conserves storage; Disadvantages: allocation/deallocation overhead, subprograms cannot be history sensitive, inefficient references (indirect addressing)
Elaboration (definition)
The process by which a declaration's executable code is executed; stack-dynamic storage bindings are created when declarations are elaborated
Explicit heap-dynamic variables (lifetime category)
Allocated and deallocated by explicit programmer directives during execution; referenced only through pointers or references, e.g., dynamic objects in C++ (new and delete), all objects in Java; Advantage: provides dynamic storage management; Disadvantage: inefficient and unreliable
Implicit heap-dynamic variables (lifetime category)
Allocation and deallocation caused by assignment statements, e.g., all variables in APL; all strings and arrays in Perl, JavaScript, and PHP; Advantage: flexibility (generic code); Disadvantages: inefficient because all attributes are dynamic, and loss of error detection
Scope of a variable
The range of statements over which it is visible
Local variables of a program unit
Variables that are declared in that unit
Nonlocal variables of a program unit
Variables that are visible in the unit but not declared there
Global variables
A special category of nonlocal variables
Scope rules of a language
Determine how references to names are associated with variables
Static scope
Scope based on program text (spatial); to connect a name reference to a variable, find its declaration by searching first locally, then in increasingly larger enclosing scopes
Static ancestors and static parent
Enclosing static scopes of a specific scope are its static ancestors; the nearest static ancestor is its static parent
Languages allowing nested subprogram definitions (nested static scopes)
Ada, JavaScript, Common Lisp, Scheme, Fortran 2003+, F#, and Python
How can a variable be hidden?
By having a 'closer' variable with the same name
Blocks
A method of creating static scopes inside program units, from ALGOL 60; legal in C and C++, but not in Java and C# (too error-prone)
The let construct (functional languages)
A construct with two parts: the first binds names to values, the second uses the names defined in the first part
let in Scheme
(LET ((name1 expression1) … (namen expressionn)) body)
let in ML
let val name1 = expression1 … val namen = expressionn in expression end;
let in F#
First part: let left_side = expression (left_side is a name or a tuple pattern); all that follows is the second part
Declaration order: C99, C++, Java
Variable declarations can appear anywhere a statement can appear; scope of all local variables is from the declaration to the end of the block
Declaration order: C#
The scope of any variable declared in a block is the whole block, regardless of the position of the declaration, but a variable still must be declared before it can be used
Variables declared in for statements (C++, Java, C#)
The scope of such variables is restricted to the for construct
Declaration vs definition in C and C++
Declarations specify just attributes; definitions specify attributes and storage; a declaration outside a function definition specifies the variable is defined in another file
Global scope in PHP
Variables implicitly declared in a function are local to the function; those implicitly declared outside functions extend from the declaration to the end of the program but skip over intervening functions; globals are accessed in a function via the $GLOBALS array or by declaring it global
Global scope in Python
A global variable can be referenced in functions, but can be assigned in a function only if it has been declared global
Problems with static scoping
In most cases too much access is possible; as a program evolves, the initial structure is destroyed and local variables often become global, and subprograms gravitate toward becoming global rather than nested
Dynamic scope
Scope based on the calling sequences of program units, not their textual layout (temporal rather than spatial); references are connected to declarations by searching back through the chain of subprogram calls
Scope example (big calls sub1, sub1 calls sub2, sub2 uses x)
Static scoping: the reference to x in sub2 is to big's x; dynamic scoping: the reference to x in sub2 is to sub1's x
Evaluation of dynamic scoping
Advantage: convenience; Disadvantages: (1) while a subprogram executes, its variables are visible to all subprograms it calls, (2) impossible to statically type check, (3) poor readability because the type of a variable cannot be statically determined
Scope vs lifetime
Sometimes closely related but different concepts, e.g., consider a static variable in a C or C++ function
Referencing environment of a statement
The collection of all names that are visible in the statement
Referencing environment in a static-scoped language
The local variables plus all of the visible variables in all of the enclosing scopes
Referencing environment in a dynamic-scoped language
The local variables plus all visible variables in all active subprograms
Active subprogram
A subprogram whose execution has begun but has not yet terminated
Named constant
A variable that is bound to a value only when it is bound to storage; advantages: readability and modifiability; used to parameterize programs
Manifest constant
A named constant whose value binding is static
Named constants in C++ and Java
Expressions of any kind are allowed, dynamically bound
const vs readonly in C#
const: values of named constants are bound at compile time; readonly: values are dynamically bound
Strong typing
Detecting all type errors