Title: Variable Lifetime and Scope
1- Variable Lifetime and Scope
- Every C variable has two characteristics
lifetime (or extent) and scope (or visibility) - C has four types of scope for variables
- local scope variables visible only inside the
block of code in which they are defined, e.g.
function parameters - global scope variables declared outside of any
block and visible throughout the source file in
which they are declared, and potentially in other
source files as well - file scope variables visible only within the
source file in which they are declared (i.e.
static variables) - class scope (or struct scope) variables that
are members of a class or struct and visible only
within that class or struct.
2Example For each variable in the following C
code has local, global, file or class scope
// file1.h class ralph public int a
void method()
// file2.cpp include file1.h extern int
a void func( int a ) int a 3 ralph
A ralph A A.method()
A.a a
// file1.cpp include file1.h int a
100 static int count 5 void
ralphmethod() a 1
?
3- C has three types of lifetimes for variables
- automatic memory is allocated to the variable
whenever the block in which it is declared is
entered, and destroyed when the block is exited - dynamic memory is allocated at run-time using
the new operator and released using the delete
operator - static memory is allocated when the program is
loaded and is destroyed only when the program
ends - The code on the following page illustrates the
use of automatic and static variables. Trace the
code to determine the output when it is run.
4int main( void ) int num1 3, num2 7
printVals( num1, num2 ) cout ltlt num1 ltlt
ltlt num2 ltlt endl printVals( num1, num2 )
cout ltlt num1 ltlt ltlt num2 ltlt endl return
0 void printVals( int num1, int num2 )
static int data 1 data num1
num2 cout ltlt data ltlt ltlt num1 ltlt ltlt
num2 ltlt endl
5Memory Organization There are four segments in
memory when a C program is running Code
segment contains the machine language code
produced by the compiler and any constant
data(e.g. const int, const double ) Static
segment contains all data corresponding to
variables that have been declared to be static.
Stack segment contains activation records (or
stack frames) that are generated when a function
is called that store - function parameters-
local automatic variables- return address (i.e.
address of instruction after function)- return
value and other book keeping information
6Heap segment the area of memory that is used for
dynamic memory allocation. Data stored in the
heap is accessed indirectly through pointers.
7int k static int i static void q ( void )
void p ( int a ) static int b 50
int c int w new int c a a b
c if ( a ! 1 ) q ()
static void q ( void ) k 1 p( k )
int main () int j int k 2 p
( k )
8int k static int i static void q ( void )
void p ( int a ) static int b 50
int c int w new int c a a b
c if ( a ! 1 ) q ()
static void q ( void ) k 1 p( k )
int main () int j int k 2 p
( k )
global
local
file
Scope of identifiers bound to variables
9int k static int i static void q ( void )
void p ( int a ) static int b 50
int c int w new int c a a b
c if ( a ! 1 ) q ()
static void q ( void ) k 1 p( k )
int main () int j int k 2 p
( k )
automatic
static
code
dynamic (heap)
Extent (lifetime) of objects
10Memory Fragmentation When a request is received
for memory to be allocated dynamically, a portion
of the heap big enough to store the data is
allocated to that data assuming that memory is
available. We know that as well as allocating
memory dynamically, we can also de-allocate that
memory using the delete operator. After a large
number of allocations and de-allocations, the
memory can become fragmented making it impossible
to allocate memory to larger data.
11Memory Allocation Strategies Memory can be
allocated using any of the threefollowing
strategies (or many others) First fit the first
block of memory largeenough to accommodate the
request is used e.g. allocate three
blocks Best fit the smallest suitable block
of memoryis used e.g. allocate three blocks
12Worst fit the largest remaining block of memory
is identified and memory is allocated from this
block e.g. allocate three blocks