Contents Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence Scope Scope and Lifetime Referencing Environments Named Constants Introduction


Categories of Variables by Lifetimes



Download 396.24 Kb.
Page7/16
Date18.02.2022
Size396.24 Kb.
#58269
1   2   3   4   5   6   7   8   9   10   ...   16
13516098

Categories of Variables by Lifetimes


Name
  • Attributes:

Address

Type

Lifetime

Value

Scope

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


Categories of Variables by Lifetimes

(1) Static variables: bound to memory cells before execution begins and remains bound to the same memory cell throughout execution.

int f(int x) {

static int a = 0;

a += 2;

printf("%d\n",a); }

f(2) --> 2 f(4) ---> 4


#include

int sum;


int fun1(int a, float b)

{

static int cnt;



int i, j;

}


  • Disadvantage: lack of flexibility (no recursion).
  • lifetime = entire program execution.

e.g., C and C++ static variables

int f(int x) {

static int a = 0

a += 2;

printf("%d\n",a); }

f(2) --> 2 f(4) --->4



Download 396.24 Kb.

Share with your friends:
1   2   3   4   5   6   7   8   9   10   ...   16




The database is protected by copyright ©ininet.org 2024
send message

    Main page