Storage Binding and Scope of Variables

Storage Bindings

Storage binding is about the allocation of a memory location to a variable.

Allocation:getting a memory cell/location from some pool of available memory cells.

Deallocation:putting a memory cell back into the pool.

The lifetime of a variable:is the time during which it is bound to a memory cell.

A memory cell may be allocated in six different places in memory as follows:

Environment Variables and command line arguments
Stack

Heap
Uninitialized Data Segment
Initialized Data Segment
Text

Categories of Variables by Lifetimes

Static variables (uninitialized/initialized data segment, text segment)

A static variableis bound to a memory location (in the initialized/uninitialized data segment) before the execution of the program begins and remains bound to the same memory location throughout the execution of the program.

Examples

(in C++):

  • Global variables without initial values are bound to memory location in the uninitialized data segment
  • Global variables with initial values and static local variables are bound to memory locations in the initialized data segment.

FORTRAN 77All variables are static

Advantages:

  • Efficiency (direct addressing, no overhead);
  • History-sensitive subprogram support.

Disadvantage:lack of flexibility (do not allow recursion)

Stack-dynamic variable (stack)

A stack dynamic variableis bound to a memory location (in the stack) when the block in which it is defined is entered, and is deallocated when the block is exited.

Examples:local variables and value parameters in C/C++.

Advantages:

  • Allows recursions,
  • Conserve storage.

Disadvantages:

  • Overhead of allocation and deallocation;
  • Subprograms cannot be history sensitive;
  • Inefficient references (indirect addressing).

Explicit heap-dynamic variable (Heap)

Memory cellsare allocated and deallocated (in the heap) by explicit statements specified by the programmer, which takes effect during the execution of the program.

Examples(in C++)

int *pt;

pt = new int;

//allocate a memory location to hold an integer value and assigns it address to pointer pt

.

.

.

delete pt;// deallocate the memory location

int *table;

table = new int [10];

/* allocate 10 consecutive memory locations to hold integer value and assigns it address of the first to pointer table */

.

.

.

delete [ ] table;// deallocate the memory locations

Advantage:provides for dynamic storage management.

Disadvantage:inefficient and unreliable.

Implicit heap-dynamic variable

Memory cells are allocations and deallocationsarecaused by assignment statements.

Example: in APL

LIST < ----- 10.2 -34.6 51.3// allocation of three memory cells

LIST< ----47// allocation of one memory cell

Exercise

  1. Indicate where each memory location of the following program is allocated, when it is allocated, and when it is de-allocated.
  2. Trace the execution of the following program (make up addresses when one is needed) by assuming that the program is executed as follows: Program 25 12

int num1 = 10, num2;

int procedure (int *, int &, int);

int main( int argn, char * argv[ ])

{

int result1, result2, val1, val2, *pt;

if (argn != 3)

return 0;

val1 = atoi (argv[ 1 ]);

val2 = atoi (argv[ 2 ]);

pt = new int;

*pt = 17;

result1 = procedure (pt, val1, val2);

num1 += num2;

cout < “\nnum1 =” < num1 < “\tnum2=” < num2 < “\tval1=” < val1

< “\tval2 =” < val2 < “\tresult1=” < result1 < “\t*pt=” < *pt;

val1 - = val2;

*pt += 3;

result2 = procedure ( &val2, val1, val1 – val2);

cout < “\nnum1 =” < num1 < “\tnum2=” < num2 < “\tval1=” < val1

< “\tval2 =” < val2 < “\tresult1=” < result1 < “\tresult2=” < result2

< “\t*pt=” < *pt;

delete pt;

return 0;

}

int procedure (int *p, int &n1, int n2)

{

static int flag = 0;

int n = 5;

num2 = 2 * num1;

flag ++;

n ++;

*p += 3;

n1 += 4;

n2 += n;

return(*p + n1 + n2 + flag );

}

Scope of a Variable

A program block is a section of a program code where variables can be defined and used.

Program blocks are specified as follows:

  • Using the left brace { and the right brace } in C/C++, Java, C#:

{ . . . }

  • Using the keywords BEGIN and END in Pascal:

BEGIN . . . END;

  • Using the declare clause in Ada:

declare Temp : Integer:

begin

Temp := First;

First := Second;

Second := Temp;

end;

Note that in Ada, the declare clause is not needed if variables are not defined in a block.

Automatic variables defined in a program block are referred to as its local variables.

Memory locations are allocated for the local variables of a program block (in the stack) when the execution of the program enters that program block and are deallocated when it exits that program block.

Program blocks may be nested in most programming languages; but procedures/functions may not be nested in C/C++, Java, or C#. They may be nested in Ada, JavaScript, and Pascal.

The scope of a variable is the range of statements in which that variable can be referenced (or is visible).

The non-local variables of a program unit (procedure of block) are the variables that are visible but not declared there.

Two methods are used to determine the scope of non-local variables (how references to names are associated with variables): static scoping and dynamic scoping.

Static Scoping (C/C++, Java, Ada, C#, Perl)

With static scoping, a name is bound to a variable as follows:

  1. First look in the program unit (procedure or block) in which the reference is found. If a declaration is found, bind that name to the variable.
  2. Otherwise, look in the immediate outer unit.
  3. Continue until a declaration is found.

Examples:page 220

Notes:

-The for structure in C++, Java, and C# allows variable definitions in their initialization expressions. The scope of these variables is restricted to the for structure.

-In C++, the scope operator is used to refer to a global variable (:: num) when there is a local variable with the same name in a program unit.

Dynamic Scoping (APL, SNOBOL 4, Perl, COMMON LISP)

Is the method of binding names to variables as follows:

  1. First look in the procedure in which the reference is found. If a declaration is found, bind that name to the variable.
  2. Otherwise, look in the subprogram in which this subprogram is called.
  3. Continue until a declaration is found.

Example: page 228.

Referencing Environments

The referencing environment of a statement is the collection of all the names that are visible in that statement.

Examples:pages 230, 231, and 232.

Consider the following C program:

void fun(void)

{

int f , b , a;/* definition 1 */

. . . . .

while( . . . )

{

int b , d , a;/* definition 2 /*

. . . <------1

while( . . . )

{

int e , b , d;/* definition 3 */

. . . <------2

}

. . . <------3

}

while( . . .)

{

int d , a , c;/* definition 4 */

. . . <------4

}

. . . <------5

}

For each of the five marked points in the above function, list each visible variable, along with the number of the definition statement that defines it.

Point 1:b, d, a (definition 2) f (definition 1)

Point 2:e, b, d (definition 3), a (definition 2) f (definition 1)

Point3:b, d, a (definition 2) f (definition 1)

Point 4:d, a, c (definition 4) f, b (definition 1)

Point 5:f, b, a (definition 1)

Consider the following skeletal C program:

void fun1 (void);

void fun2(void);

void fun3(void);

int main()

{

int f , b , a;

. . .

return 0;

}

void fun1()

{

int b , d , a;

. . .

}

void fun2()

{

int e , b , d;

. . .

}

void fun3()

{

int d , a , c;

. . .

}

Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during the execution of the last function called? Include with each visible variable the name of the function in which it is defined: main calls fun1; fun1 calls fun2; fun2 calls fun3.

d, a, c (fun3), b, e (fun2), f (main ).

Given the following program,

a)what value of variable x is printed in procedure sub1, if static scoping rule is used.

b)what value of variable x is printed in procedure sub1, if dynamic scoping rule is used.

program main;

var x : integer;

procedure sub1;

begin{ sub1 }

writeln( ‘x = ‘ , x)

end; { sub1 }

procedure sub2;

var x : integer;

begin{ sub2 }

x := 5;

sub1

end; { sub2 }

begin{ main }

x := 10;

sub2

end. { main }

a)x = 10

b)x = 5

Exercise:5, 6, 7, 8, 9, 10, 11, 12,pages 236-240.