130 likes | 135 Views
Learn about variable lifetimes, static, stack-dynamic, and heap-dynamic bindings. Understand the importance of variable scope and how it impacts program execution. Examples from various programming languages included.
E N D
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 หรือ block แต่ไม่ได้ประกาศที่นั่น
Static Scope • ภาษา ALGOL60 เป็นภาษาแรกที่นำเสนอ scope สำหรับตัวแปรที่เป็นแบบ nonlocal แบบนี้ (ภาษา imperative อื่นได้นำแนวคิดไปใช้ด้วย) • ตัวแปรถูกกำหนด scope ก่อนการปฏิบัติงานอย่างชัดเจน • ตัวอย่าง subprogram ของภาษา Pascal
Static Scope procedure big; var x : integer; procedure sub1; begin … x … end; procedure sub2; var x : integer; begin … end; begin … end; Static parent hidden
Block • ภาษาแรกที่ใช้คือ ALGOL60 • เป็นรูปแบบ static scope ที่ทำให้ส่วนหนึ่งของโค๊ดเป็นเจ้าของตัวแปรได้ • ตัวแปรจะมีพื้นที่ในหน่วยความจำเมื่อเข้าสู่ section และคืนพื้นที่เมื่อออกจาก section (เรียก section นี้ว่า block) if (i<j){ int temp; temp = i; i = j; j = temp }
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("%d\n", i); }
Dynamic scoping • ภาษาที่เป็นแบบนี้เช่น APL, SNOBOL4, LISP • Scope เกิดขึ้นจากลำดับการเรียก (calling sequence) ของ subprogram • procedure big; • var x : integer; • procedure sub1; • begin • … x … • end; • procedure sub2; • var x : integer; • begin • … • end; • begin • … • end; สมมุติเป็นแบบ dynamic scoping ถ้า big เรียก sub2 และ sub2 เรียก sub1 ใน sub1 อ้างถึง x ?
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.