Download presentation
Presentation is loading. Please wait.
Published byRhoda Jacobs Modified over 8 years ago
1
Chapter 5 Names, Bindings, Type Checking CSCE 343
2
Storage Bindings & Lifetime Allocation: bind variable to memory cell from available pool Deallocation: putting cell back into available pool Lifetime: the time during which it is bound to cell For scalar variables there are four categories –Static –Stack dynamic –Heap dynamic –Implicit heap dynamic
3
Static Bound to memory cells before execution begins Remains until program ends. –Advantage: efficiency, no run time allocation and deallocation –Allows subprograms to have variables that are historically sensitive (retain value between calls) –Disadvantage: lack of flexibility (no recursion) needs more RAM since subprograms are allocated memory at compile time (e.g., two subprograms that use large local arrays)
4
Stack-Dynamic Storage is created and allocated when declaration statements are executed If scalar, all attributes except address are statically bound –Local variables in C and Java subprograms Advantage: recursion, conserves storage –Two functions that both have large arrays Disadvantages: –Overhead of allocation/deallocation –Suprograms are not history sensitive
5
Explicit Heap-Dynamic Allocated (and deallocated) by explicit directives Take effect during execution Referenced only through pointers (or references) –All objects in Java (r = new Random()) –Dynamic objects in C++ Advantage: dynamic storage management Disadvantage: inefficient, complexity of management, difficulty of using pointer/reference variables correctly
6
C++ //Java Explicit Heap Dynamic C++ int *intPtr; intPtr = new int; delete intPtr; Java Integer intRef; intRef = new Integer(); intRef = ???; C++ does not have automatic garbage collection
7
Implicit Heap-Dynamic Allocation/Deallocation caused by assignment statements –All variables in APL; strings and arrays in Perl and JavaScript myList = [14, 15, 18, 15] Advantages: flexibility Disadvantages: –Inefficient, keeping track of dynamic attributes –Loss of error detection
8
Type Checking Type checking: activity of ensuring that the operands of an operator are compatible type –Compatible type: Legal for the operators or Allowed under language rules to be implicitly converted (coersion) Type error: application of an operator to an inappropriate operand type –X * Y –Include assignment: x = expression –Include subprograms: foo(int x, int y) Operator foo, operands x and y
9
Type Checking Static type bindings: static checking Dynamic type bindings: dynamic checking Strongly typed language: –“All” type errors are detected –Advantage: allows detection of misuse of variables
10
Strong Typing Strongly typed languages: –C, C++ (almost; they have a union type that is not type checked) –Java (explicit casts are allowed, but caught at run-time) Coercion rules weaken strong typing –Logical errors may be missed widthFactor = (size/numPix) * 1.25 –Java has just half of the coercions of C++
11
Type Compatibility Type compatibility rules for primitive types are straight forward and include coercion Type compatibility rules for structures and user defined types are not so simple –Use strict compatibility called equivalence Name type equivalence: 2 vars are equivalent if they are in same declaration or use same type name in their declaration –Highly restrictive: subranges are not compatible (double, float) Structure type equivalence: 2 vars are equivalent if types have identical structures –More flexible, but harder to implement
12
C++ Example typedef map MapType; map myMap1; MapType myMap2; Are myMap1 and myMap2 compatible? Are they name type equivalent? Are they structure type equivalent?
13
Scope The scope is the range of statements over which a variable is visible (i.e., the variable can be reference). –Scope rules of a language determine how names are associated with variables. –Static scoping and dynamic scoping Non-local variables: those that are visible in a unit, but not declared there.
14
Static Scope Based on program text, can be determined prior to execution. Searching for variable declaration (attributes): –Start local, then look in larger enclosing scopes –Nearest enclosing scope: static parent –Enclosing scopes: static ancestors Variables hidden by same name, but “closer” –Can sometimes get access through other means: C++: class_name::name Java: this.name
15
Static Scoping main can call A and B A can call (B?), C and D B can call A and E MAIN E A C D B AB CDE
16
Static Scoping MAIN AB CDE A C B ED Intended Calls Possible Calls What if we wanted E to have access to D
17
Static Scoping To give E access to D –move D to same level as A and B –make some of A’s variables global so D has access Disadvantages: –Data access and desired call structure –Overall: encourages many global variables
18
Dynamic Scope Based on calling sequence (not code layout) and scope determined at run time –dynamic parent: immediate calling procedure –dynamic ancestor: procedure on the runtime stack Search back through chain of calls (stack) A reference to a non-local variable, x, may refer to different variables at different points in time Problems –local variables are visible to all directly or indirectly called procedures –readability –takes more time to determine binding
19
Scope Example Static scope for variable x in C Dynamic scope for variable x is –A calls B calls C –A calls C procedure A is x:Integer; procdedure B is x:Integer begin end; procdure C is begin … x… end; begin //A end; //A
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.