Chapter 3: Names, Scopes and Bindings
What is a referencing environment?
A set of active bindings in the current environment; determined by static/dynamic scope rules
List the objects and information commonly found in stack frame.
Arguments, return values, local variables, return address, caller's stack frame
What are forward references? Why are they prohibited or restricted in many programming languages?
Attempting to refer to a name before it is declared Simplify the compiler
Why are binding rules particularly important for languages with dynamic scoping?
Binding is determined at runtime so binding rules are necessary to determine this?
Why does the use of dynamic scoping imply the need for runtime type checking?
Cannot be checked at compile time; behavior of program determine the bindings
How do classes differ from modules?
Classes are data abstractions; need to create instances of them, design a hierarchy of classes Modules separate implementation/functionality; do not want multiple instances of them
What is a static chain? What is it used for?
Creating chains to refer to the previous frame of the stack from which the subroutine was called
Explain the difference between a declaration and a definition. Why is the distinction important?
Declaration: introduces the name; tells compiler the size, type Definition: the implementation details; what the linker needs Must have one definition per declaration
What is the advantage of binding things early as possible? What is the advantage of delaying the binding?
Early bindings: greater efficiency Later bindings: more flexibility
What are macros? What was the motivation for including them in C? What problems may they cause?
Ease the burden of highly repetitive code in writing assembly No type checking: can cause errors ex) i++ * i++
Why might it be useful to distinguish between a header and the body of a module?
Header: declaration part Body: implementation part Can compile as soon as header exists; does not depend on the body
Explain the importance of information hiding
Hides objects/algorithms not used; reduce cognitive load of the programmer, reduces name conflicts
What is garbage collection?
Identify and reclaim unreachable objects in heap
What are internal and external fragmentation?
Internal: wasted space within each block External: used blocks are scattered throughout the heap, so if an object requires multiple blocks, there might not be consecutive free blocks to fit the object
Differentiate lifetime of a name-to-object binding and its visibility
Lifetime: time between creation and destruction of a binding; storage duration Visibility: its scope, when the object could be accessed
What are aliases? Why are they considered a problem in language design and implementation?
Multiple names that refer to the same object; confusing and can prevent compiler from creating optimizations
Explain the closest nested scope rule.
Name is known in scope of which it was declared, and in each internally nested scope
What is overloading? How does it differ from coercion and polymorphism?
Overloading: the same name that refers to multiple implementations; its arguments determine which implementation to use Coercion: compiler converts a type to another Polymorphism: subroutine behaves differently based on the type of the object calling the subroutine
What is a subroutine closure? What is it used for? How is it implemented?
Passing a subroutine with context Treated as a class with fields, holding information about the reference environment of the subroutine
What is a dangling reference?
Reference to an object that no longer exists
What is an opaque export?
Refers to types; variables of that type may be declared, passed as arguments, but not modified
Explain the purpose of a compiler's symbol table.
Semantic analysis; check if identifiers have been declared, correct types, parameters of functions, keeps track of scope
Why might it be useful to have modules and classes in the same language?
Separate functionalities through modules and multi-instance abstractions
Explain the difference between decisions that are bound statically and those bound dynamically.
Static: before runtime Dynamic: at runtime
Describe the difference between static and dynamic scoping.
Static: can determine scope just by looking at the source program Dynamic: Bindings depend on the flow of execution at run time
What determines whether an object is allocated statically, on the stack, or in the heap?
Static: global variables, machine code; have absolute address that is retained throughout the program's execution Stack: allocated/deallocated LIFO, in conjunction with subroutine calls and returns Heap: at arbitrary times
What are first-class subroutines? What languages support them?
Subroutines that can be passed as a parameter, returned, or assigned to a variable Functional languages
What is a calling sequence?
Subroutines that maintains the stack; code that executes before a call, beginning of call, and at end of call
What is binding time?
The time when a binding, an association between two things, is created (or more generally, a time when an implementation decision is made) ex) times: compile-time, run-time, link-time
Explain the distinction between limited and unlimited extent of objects in a local scope.
Unlimited: lifetimes continue indefinitely Limited: destroyed at the end of scope
What is a frame pointer? What is it used for?
Used to set up the stack when subroutine is called; refer to items on the stack; used to return from the function; points to the bottom of the stack (above the previous stack frame) This is not the same as the stack pointer!! (which points to the next unused location on stack)
What is elaboration?
When a variable is allocated space (onto stack for example), initialize values; creation of bindings
What is the purpose of a scope resolution operator?
When variables are inaccessible despite being in nested scope, can access outer meanings of a name Gives the correct name based on the provided context. For example, C++ uses :: operator, std::cout