Download CS421 Lecture 9: Understanding Names, Variables, and their Attributes and more Study notes Computer Science in PDF only on Docsity!
Names and Variables Binding Binding Categories Scope
CS421 Lecture 9: Names and Variables
Mark Hills
University of Illinois at Urbana-Champaign
June 17, 2008
1 Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, and Elsa Gunter
Names and Variables Binding Binding Categories Scope
(^1) Names and Variables
(^2) Binding
(^3) Binding Categories
4 Scope
Names and Variables Binding Binding Categories Scope
Overview Variable Attributes
Names
Def: A name is a string of characters used to identify some entity
in a program^2
Names (or identifiers) give us a way to name program
concepts, such as types, values, classes, functions, etc.
Different languages have different rules for names
Case-sensitive (C, C++, Java) or not (Pascal, Lisp)?
Are there special words in a language that are reserved words,
or are there just keywords
Naming requirements/conventions
Name length and allowed characters
2 Sebesta,2nd edition,p.
Names and Variables Binding Binding Categories Scope
Overview Variable Attributes
Variables
A variable is just an abstraction of memory. We can define a
variable as a sextuple of attributes:
Name
Address
Value
Type
Lifetime
Scope
Names and Variables Binding Binding Categories Scope
Overview Variable Attributes
Variables: Address
The address is the machine memory address that is associated with
the variable. This can be trickier than it sounds:
Multiple variables may refer to the same address – aliasing
pointers and references
Equivalence-type statements and unions
function calls
The same name may have multiple addresses at different
times
OS-based – different physical address, same virtual address
language-based – variables in functions may have different
addresses on each call
Sometimes called an l-value, since a variable’s address is used on
the left-hand side of an assignment.
Names and Variables Binding Binding Categories Scope
Overview Variable Attributes
Variables: Types
The type of a variable determines what kinds of values the variable
can refer to. We will cover what exactly we mean by type in a later
lecture; for now, think of it as a (possibly infinite) set of values.
Names and Variables Binding Binding Categories Scope
Binding Binding Times Static and Dynamic Declarations, Storage, and Lifetime
Binding
When we talk about associating attributes with variables, we are
talking about binding.
Def: In the general sense, a binding is an association
between two items, such as varibles and attributes
Def: The time this association occurs is the binding time
Names and Variables Binding Binding Categories Scope
Binding Binding Times Static and Dynamic Declarations, Storage, and Lifetime
Binding Times
Binding can take place at:
Language design time (identifying ∗ as multiplication)
Language implementation time (an int takes 4 bytes)
Compile time (variable x is an int)
Load time (variable x is in storage location 10)
Link time (function f is in load module m)
Run time (x has the value 10)
Names and Variables Binding Binding Categories Scope
Binding Binding Times Static and Dynamic Declarations, Storage, and Lifetime
Static vs. Dynamic Bindings
Bindings are either static or dynamic:
Static bindings first occur before run time and remain
unchanged during program execution
Dynamic bindings either occur during run time or occur earlier
but can be changed during run time
An example of a dynamic binding is dynamic typing, where the
type is either not computed prior to execution or can change
during execution.
Names and Variables Binding Binding Categories Scope
Binding Binding Times Static and Dynamic Declarations, Storage, and Lifetime
An Aside: Constants
Constants are similar to variables except the value, once assigned,
cannot be changed.
Usually they can be allocated statically
Some languages allow constants that are defined once at
runtime but then not changed (sometimes called readonly)
Literals can be thought of as constants (i.e. 5, “hello”, etc.)
Names and Variables Binding Binding Categories Scope
Binding Binding Times Static and Dynamic Declarations, Storage, and Lifetime
Storage and Lifetime
Def: Taking memory from the pool of free memory and
assigning it to a variable is allocation
Def: Returning this memory to the free pool is deallocation
The lifetime of a variable is the time between the initial
allocation and the future deallocation
In some languages, assigning storage for a variable also provides a
starting value. In others, the variable has a random starting value,
based on the existing memory contents.
Names and Variables Binding Binding Categories Scope
Bindings and Variable Categories
We can separate binding of scalar (unstructured) variables into
four categories, to make them easier to organize and compare:
Static Variables
Stack-Dynamic Variables
Explicit Heap-Dynamic Variables
Implicit Heap-Dynamic Variables
Names and Variables Binding Binding Categories Scope
Stack-Dynamic Variables
Stack-dynamic variables are those whose storage binding are
created when their declarations are elaborated – when the binding
process indicated by the declaration is executed. This then occurs
at run-time.
Example: variables at the start of a method declaration are
elaborated when the method is invoked and then deallocated
when the method exits
Storage is taken from the stack
This is the default in many languages
Ease of allocation and space management are advantages
...while cost of indirect lookups (generally off a stack or frame
pointer) is a disadvantage
Names and Variables Binding Binding Categories Scope
Explicit Heap-Dynamic Variables
Explicit heap-dynamic variables are nameless variables created
explicitly by run-time calls in a program – for instance, with
malloc or new.
Explicit heap-dynamic variables are allocated from the heap
They do not have names, but instead a pointer or reference to
this variable is saved into another variable – note this means
we have two variables!
Some languages include explicit deallocation as well, while
others use garbage collection
Advantage: storage requirements do not need to be known
before the program starts
Disadvantage: heap management is tricky and can be slower