Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scope, Visibility, and Lifetime

Similar presentations


Presentation on theme: "Scope, Visibility, and Lifetime"— Presentation transcript:

1 Scope, Visibility, and Lifetime

2 Variables can be bound to a scope either statically or dynamically.
Scope, Visibility, and Lifetime: To reuse the same Identifier within a program requires the use of scope, hiding, and visibility. The scope of a variable is the range of program statements that can access that variable. A variable is visible within its scope and invisible or hidden outside it. Variables can be bound to a scope either statically or dynamically.

3 Static Scope: Static Scope defines the scope of a variable in terms of the lexical structure of a program. Using Static Scope each reference to a variable is statically bound to a particular (implicit or explicit) variable declaration. All variable references can be resolved by looking at the program’s source code and is independent of execution. Static scope rules are used by most traditional imperative programming languages (examples?).

4 Dynamic Scope: Dynamic Scope defines the scope of a variable in terms of program execution. Each variable declaration extends its effect over all subsequent statement execution, until a new declaration for the identifier is encountered. Dynamic scope rules are easy to implement but have drawbacks (such as?). Dynamic scope is most often used by interpreted languages; APL, LISP, and SNOBOL are examples.

5 The action that acquires storage for a variable is called allocation.
Lifetime: The lifetime of a variable is the interval of time in which storage is bound to the variable. The action that acquires storage for a variable is called allocation. Some languages allocate storage before run-time. This is called static allocation. Others allocate storage at run-time either using explicit requests (malloc, new) or automatically upon entering a variable’s scope. This is called dynamic allocation. Languages may use both methods.

6 The scope of variables can be global or local.
Identifier reuse: The scope of variables can be global or local. A global variable’s scope includes all the statements in a program. The scope of a local variable includes only statements inside the function in which it is declared. The same identifier can be reused inside different functions to name different variables. A name is local if it is declared in the current scope, and it is global if declared in an outer scope.

7 A global identifier can be redeclared inside of a function.
Identifier reuse: A global identifier can be redeclared inside of a function. The new, inner declaration is said to hide the old, outer declaration (what effect does this have?). Some languages provide a scope resolution mechanism for referencing hidden variables. Some languages provide explicit hiding and exporting of variables and functions/methods in order to control visibility (examples?).

8 Example Program with Functions and Parameters
Figure 4.5 Variables i and j as well as parameters x and y are local to A. Variables declared in B and main are not visible in A. The variables h and i are global. h is visible in A but i is hidden by the local declaration of i.

9 What constitutes a scope varies among languages.
What is a Scope? What constitutes a scope varies among languages. Some allow scopes to be nested; every block or statement can contain declarations. Some allow declarations anywhere within a block, some only prior to any executable statements. Some allow function declarations to be nested (Pascal does, C does not). Some are even more complicated with special rules for some types of statements (Ada).

10 C/C++ allows declarations to be present in statements:
Scope Example: C/C++ allows declarations to be present in statements: If (a[j] > a[k]) { int t = a[j]; a[j] = a[k]; a[k] = t; }

11 C/C++ allows declarations to be present in statements (2):
Scope Example: C/C++ allows declarations to be present in statements (2): for (int i=0; i<10; i++) { sum = a[i]; } j = i; Note that what to do with loop variables has been a topic of much debate in the past.

12 Object oriented languages rely heavily on overloading.
Early languages required that a locally defined identifier be unique in its scope (Fortran, Pascal, and C). Later languages permitted names to be overloaded, allowing multiple definitions of the same identifier within the same scope (Ada, C++). This is allowed as long as references can be resolved from the context the identifier is used in. Object oriented languages rely heavily on overloading.

13 Overloading Example: Java allows both instance variables and methods to have the same name within a single class: class Overload { int name; // an instance variable int name () {…} // a method name = name(); } Why is this possible?

14 New Abstract Syntax Rules for Jay with Methods and Globals
Figure 4.6

15 Abstract Syntax Sketch for a Jay Program with Globals and Methods
Figure 4.7

16 Next time… Memory Management


Download ppt "Scope, Visibility, and Lifetime"

Similar presentations


Ads by Google