Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Programming in C

Similar presentations


Presentation on theme: "Advanced Programming in C"— Presentation transcript:

1 Advanced Programming in C
Scope, Visibility, and Lifetime

2 Scope determine the lifetime and visibility of variable.
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 part of program or block of program in which it is visible. A variable is visible within its scope and invisible or hidden outside it. Lifetime of variable define the time for which the variable exists in a program. Scope determine the lifetime and visibility of variable. Variables can be bound to a scope either statically or dynamically.

3 Variable can be bound at compile time without regards to calling code.
Static Scope (based on the text of the program): 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. Variable can be bound at compile time without regards to calling code. 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: Algol, Ada, C, Pascal).

4 Output : 4, 0 and 4. { int x = 0; void fie(int n){ x = n+1; } fie(3);
Static Scope (Example): {     int x = 0;     void fie(int n){         x = n+1;     }     fie(3);     write(x);     {         int x = 0;         fie(3);         write(x); } Output : 4, 0 and 4.

5 Output : 2 Static Scope (Example): int X = 0; int Y; void fie(){ X++;
} void foo(){     fie(); read(Y); if Y > 0{     int X = 5;  // line 12     foo();     } else  write(X); Output : 2

6 Variable binding can be determined at the moment code is executed.
Dynamic Scope (based on the flow of execution): 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. Variable binding can be determined at the moment code is executed. Dynamic scope rules are easy to implement but have drawbacks. Dynamic scope is most often used by interpreted languages; APL, LISP, and SNOBOL are examples. Dynamic scope is based on the flow of execution, so the father block of a block is the one which call the block, but not where is the block in the file.

7 Dynamic Scope (Example):
{     const x = 0;     void fie()         write(x);   }     void foo()         const x =1;         fie();     }     foo(); } The foo() call the fie(), and the father block of fie block is foo, so the output x is the one which is defined in the foo, whose value is 1. So the Output : 1

8 Output : when y > 0 output is 1, when y < 0, output is 0
Dynamic Scope (Example): int X;         // line 1 X = 1; int Y; void fie() { foo();      X = 0; } void foo() {      int X;     X = 5; read(Y); if Y > 0  int X;     // line 14      X = 4;    fie(); else     fie(); write(X); Output : when y > 0 output is 1, when y < 0, output is 0

9 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. There are three lifetime in C : Static, Automatic and Dynamic

10 int foo() {     static int count = 0;     // "count" has static lifetime     int * counter = malloc(sizeof(int));     // "counter" has automatic lifetime     free(counter);     // malloc’ed memory has dynamic lifetime } The variable count has a static lifetime, i.e., its lifetime is that of the program. The variable counter has an automatic lifetime — its life is till the function returns; it points to a heap-allocated memory block — its life remains till it is explicitly deleted by the program, which is not predictable, and hence it has a dynamic lifetime.

11 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.

12 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. Some languages provide a scope resolution mechanism for referencing hidden variables.

13 // the "i" variable is accessible/visible here void foo() { int i;
Identifier reuse: int i; // the "i" variable is accessible/visible here void foo() {     int i;     // the outer "i" variable     // is not accessible/visible here     {         int i;         // two "i" variables at outer scopes         // are not accessible/visible here     }     // the "i" in this block is accessible/visible     // here and it still hides the outer "i" } // the outermost "i" variable //is accessible/visible here

14 Example Program with Functions and Parameters
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.

15 Scope varies among languages.
What is a Scope? 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).

16 Scope Example: There are five scopes in C: program, file, function, block, and prototype void foo() {} // "foo" has program scope static void bar() {     // "bar" has file scope     printf("hello world");     int i;     // "i" has block scope } void baz(int j); // "j" has prototype scope print: // "print" has function scope

17 Scope Example: The foo function has program scope. All non-static functions have program scope, and they can be called from anywhere in the program and available throughout the program. the function needs to be first declared using extern. The function bar has file scope — it can be called from only within the file in which it is declared. It cannot be called from other files, unlike foo. The label print has function scope. There can be only one print label inside a function, and you can write a goto print statement anywhere in the function, even before the label appears in the function. The variable i has block scope. We can define another variable with the same name i inside another block within the bar function. The variable j has prototype scope: you cannot declare any other parameter with the same name j in the function baz. Scope of j ends with the prototype declaration.


Download ppt "Advanced Programming in C"

Similar presentations


Ads by Google