Names Scopes and Bindings The Concept of Binding
Names, Scopes and Bindings �������
The Concept of Binding • Categories of variables by lifetimes – Static • bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. e. g. all FORTRAN 77 variables, C static variables • Advantages: efficiency (direct addressing), history-sensitive sub program support • Disadvantage: lack of flexibility (no recursion)
The Concept of Binding • Categories of variables by lifetimes – Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. • If scalar, all attributes except address are statically bound – e. g. local variables in C and Java • Advantage: allows recursion; conserves storage • Disadvantages: – Overhead of allocation and deallocation – Subprograms cannot be history sensitive – Inefficient references (indirect addressing)
The Concept of Binding • Categories of variables by lifetimes – Explicit heap-dynamic--Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution • Referenced only through pointers or references e. g. dynamic objects in C++ and all objects in Java • Advantage: provides for dynamic storage management • Disadvantage: inefficient and unreliable
Scope • ����������� • Def: The scope of a variable – ������� statement ���������� • Def: The local variables – ���������� subprogram ���� block • Def: The nonlocal variables – ���������� subprogram
Static Scope procedure big; var x : integer; procedure sub 1; begin …x… end; procedure sub 2; var x : integer; begin … end; Static parent hidden
Block #include <stdio. h< int i = 0 ; int main} () printf("%d ", i; ( } int i = 1 ; printf("%d ", i; ( } int i = 2 ; printf("%d ", i ; ( i ; ++ printf("%d ", i ; ( { printf("%d ", i; ( { printf("%dn", i; ( {
Dynamic scoping • ����������� APL, SNOBOL 4, LISP • Scope ���������� (calling procedure ��� big; subprogram sequence) var x : integer; procedure sub 1; begin …x… end; procedure sub 2; var x : integer; begin … end; ������� dynamic scoping ��� big ����� sub 2 ����� sub 1
Scope and lifetime • Lifetime ��������� scope �������� if (i<j){ int temp; temp = i; i = j; j = temp } void printheader(){ … } void compute(){ int sum; … printheader();
����� • Concepts of programming languages : Sebesta, Robert W.
- Slides: 12