Variables Scope and Binding Ch.5 CPL

Module 4 Study Guide
Learning Objective
  • Understand what a variable is
  • List the six attributes of a variable
  • Eexplain what the name, address, value, and type attributes are
  • Recognize the conventions governing naming in programming languages
  • State the difference between keywords and reserved words
  • Explain what bindings and binding times are and give examples of important bindings and binding times
  • Distinguish between static and dynamic bindings
  • Distinguish between static and dynamic typing, give examples of languages with staticand dynamic typing, and explain their relative advantages and disadvantages
  • Explain what memory allocation is
  • Define the lifetime of a variable
  • Distinguish the four variable lifetime categories of static, stack-dynamic, explicit, heap-dynamic, and implicit heap-dynamic
  • Give examples of variables with each of the four variable lifetime categories
  • Explain the regions where memory is allocated for each of the four variable lifetime categories
  • Explain what variable with different lifetimes are used for, and identify languages thatprovide variables in each variable lifetime category
Although your text does not discuss it, the lifetimes of variables have implications for how memory is laid out during program execution. Static variables (along with constant values)occupy a region of memory called static memoryThis portion of memory has a size that does not change during program execution. Stack-dynamic variables are allocated and deallocated from a region of memory called the runtime stack or just the stackThe stack grows and shrinks during execution, and sometimes it grows so large that it exhausts the region of memory reserved for it. In this case the program may crash, or perhaps a larger regions will be allocated for the stack. If the stack grows to the point where it exhausts all available memory, then the program will crash. This occurs, for example, when a faulty recursive function fails to stop calling itself. Finally, heap-dynamic variables occupy a region called the heap or the free store. Variables are allocated and deallocated during execution. If this region is exhausted, then garbage collection may be able to reclaim space.
In summary, a language with only static variables (like early FORTRAN) needs only a static region. A language with recursion but no heap-dynamic variables (like Algol) needs a static region and a run-time stack. A language with variables of all three sorts of lifetimes(like most modern languages) needs a static region, a runtime stack, and a heap.

Reading notes:
  • Understand what a variable is
    • A readable word that represents a memory location and its contents
  • List the six attributes of a variable
    • type, value, name, address, scope, lifetime
  • Explain what the name, address, value, and type attributes are
    • value is the actual data stored in memory
    • address is the location of that data
    • name is a word that connects to an address through a table
  • Recognize the conventions governing naming in programming languages
    • Camel case is king now
    • case sensitivity is annoying but Java enforces it by design
  • State the difference between keywords and reserved words
    • Keywords can be used as variables while reserved words can’t
    • Integer apple vs. Integer = 5
    • If Integer is a keyword, both of those are fine
  • Explain what bindings and binding times are and give examples of important bindings and binding times
    • Binding is the association of two entities and can happen at design time, compile time, run time etc.
    • Design time binding is stating that * is associated with multiplication
    • Runtime binding assigns 5 to the value of variable count
    • Compile time associates the type of int to a variable count
    • Conpile design time associates a uint_8 with the binary representation of what a uint_8 is
  • Distinguish between static and dynamic bindings
    • Dynamic bindings happen at runtime
    • Dynamic typing in javascript allows for any assignment to any variable.
    • The variable being assigned to will change its type
    • Dynamic typing is exspensive since it can’t be compiled and can only run through an interpreter
      • Compilers can make machine code for dynamically typed programs since they don’t know
  • Distinguish between static and dynamic typing, give examples of languages with static and dynamic typing, and explain their relative advantages and disadvantages
    • Static typing forces the type of variables to be explicitly declared and not changed
    • Inference typing uses either context or name to assign a type
      • var = 5 will have an implicit type of int
      • var = 5.0 will have an implicit type of float
    •   Perl uses the name to determine declared type
      • $var is scalar
      • @var is array
      • %var is hash
  • Explain what memory allocation is
    • Defining that a specific memory cell is in use for a specified variable. It’s liek reserving a room
  • Define the lifetime of a variable
    • The time a variable is assigned to a specific memory location – allocation and deallocation
  • Distinguish the four variable lifetime categories of static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic
  • Give examples of variables with each of the four variable lifetime categories
    • global variables are static and declared early
    • stack-dynamic are parameters that get passed to a function, We don’t know how large the array will be so it is dynamic
    • exlpicit heap dynamic is using the keyword new to create a new object
    • implicit heap dynamic is javascript assignments where new arrays are put onto the heap at runtime and no type is explicitly declared
      • They are dynamic because they aren’t put into the heap until values are assigned
  • Explain the regions where memory is allocated for each of the four variable lifetime categories
    • static values can be put anywhere where memory is constant for the program execution (Global)
    • stack dynamic are located on the runtime stack
    • heap stuff is put onto the heap
  • Explain what variable with different lifetimes are used for, and identify languages that provide variables in each variable lifetime category

Ruby has runtime type binding and supports implicit heap variables as does javascript.