Variables - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Variables

Description:

Variables Names Binding Types and Type Checking Scope and Lifetime Variable Names Syntactic restrictions Maximum (character) length Valid characters Alphanumeric ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 18
Provided by: elvisRow
Learn more at: http://elvis.rowan.edu
Category:
Tags: c | variables

less

Transcript and Presenter's Notes

Title: Variables


1
Variables
  • Names
  • Binding
  • Types and Type Checking
  • Scope and Lifetime

2
Variable Names
  • Syntactic restrictions
  • Maximum (character) length
  • Valid characters
  • Alphanumeric, connectors, etc.
  • Case-sensitivity
  • Reserved words

3
Variables
  • What is a variable?
  • Abstraction of computer memory cell or collection
    of cells
  • Assembly Language Machine Language names
  • Tuple of attributes
  • Name
  • Address
  • Value
  • Type
  • Lifetime
  • Scope

4
Variable Address
  • Not necessarily exclusive
  • Many variables can refer to same
    address(aliasing)
  • Not necessarily constant
  • Variables often refer to different addresses in
    lifetime of program (e.g., recursive function)
  • L-value

5
Variable Type
  • Range of values a variable can have
  • Can be dictated by machine limitations(e.g., C
    short)
  • Often enforced by compiler/interpreter
  • Strong typing vs. latent (or dynamic) typing

6
Variable Value
  • Contents of (possibly abstract) memory cell
    addressed by variable
  • R-value

7
Variable Binding
  • Binding the association between variable and
    value
  • Binding time when the association takes place
  • Language-definition time (e.g., in C)
  • Compiler-design time (e.g., int in C)
  • Compile-time (e.g., int count 5)
  • Run-time (e.g., count count 5)

8
Binding Attributes
  • Static vs. dynamic
  • Can binding change over lifetime of program?
  • Type
  • Can be set explicitly or implicitly
  • Consider FORTRAN, BASIC, Perl
  • Dynamic type binding Scheme, LISP, APL
  • Type Inference ML, Miranda, Haskell

9
Variable Binding Lifetime
  • For how long (in programs execution) does
    binding persist?
  • Categories
  • Static
  • Stack-dynamic
  • Explicit heap-dynamic
  • Implicit heap-dynamic

10
Variable Binding Lifetime (2)
  • Static Binding
  • Binding made before program execution, remain
    bound until program termination.
  • Type, address are static
  • Efficiency memory reference can be calculated at
    compile-time
  • Cannot be used with recursion

11
Variable Binding Lifetime (3)
  • Stack-dynamic
  • Type is static, address is dynamic
  • Directly supports recursion
  • Efficiently supported by run-time stacks
  • Not necessarily as efficient as static variable
    bindings

12
Variable Binding Lifetime (4)
  • Explicit Heap-Dynamic
  • Storage explicitly allocated/deallocated out of
    memory pool at run-time
  • Useful for dynamically-sized structures (e.g.
    linked-lists, stacks)
  • Programming complexity to maintain
    integrity(e.g., dangling references,
    array-bounds checking)

13
Variable Binding Lifetime (5)
  • Implicit Heap-Dynamic
  • Memory storage allocated when value is assigned
  • Very flexible
  • Reduced programmer complexity
  • Run-time overhead (array bounds checking, garbage
    collection)

14
Variable Scope
  • Range of program statements in which variable is
    visible.
  • Static scoping (lexical scoping)Scope can be
    determined at compile time
  • Dynamic scopingScope depends on execution

Lifetime ? When Scope ? Where
15
Lexical vs. Dynamic Scoping
  • (define a 12)
  • (define add-a
  • (let ((a 45))
  • (lambda (n) ( n a)))
  • (let ((a 12))
  • (add-a 15))
  • Lexical 60
  • Dynamic 27

16
Namespaces Functions vs. Data
  • Many languages (e.g. C, Common Lisp) maintain
    separate namespaces for functions and data names
  • Often have different scoping and lifetime rules
    too

17
Garbage Collection
  • Practically essential for systems with implicit
    heap-dynamic object
  • Garbage anything not reachable from known
    roots.
  • Roots Current stack current variable bindings
Write a Comment
User Comments (0)
About PowerShow.com