8 - Lifetime. Scope Memory.pdf-CPSC 260 ...
8_-_Lifetime._Scope_Memory.pdf-CPSC 260 Lifetime, Scope and Memory
Showing 2 out of 2
8 - Lifetime. Scope Memory.pdf-CPSC 260 Lifetime, ...
8_-_Lifetime._Scope_Memory.pdf-CPSC 260 Lifetime, Scope and Memory
8 - Lifetime. Scope Memory.pdf-CPSC...
8_-_Lifetime._Scope_Memory.pdf-CPSC 260 Lifetime, Scope and Memory
Page 2
CPSC 260
Lifetime, Scope and Memory Organization
Page 7
int 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 );
}
CPSC 260
Lifetime, Scope and Memory Organization
Page 8
int 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
CPSC 260
Lifetime, Scope and Memory Organization
Page 9
int 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
dynamic (heap)
code
Extent (lifetime) of objects
CPSC 260
Lifetime, Scope and Memory Organization
Page 10
Memory 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.
CPSC 260
Lifetime, Scope and Memory Organization
Page 11
Memory Allocation Strategies
Memory can be allocated using any of the three
following strategies (or many others):
First fit
:
the first block of memory large
enough to accommodate the request is used
e.g. allocate three “blocks”
Best fit
:
the smallest suitable block of memory
is used
e.g. allocate three “blocks”
CPSC 260
Lifetime, Scope and Memory Organization
Page 12
Worst fit
:
the largest remaining block of memory is
identified and memory is allocated from this block
e.g. allocate three “blocks”


Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
UBC Vancouver
UBC_Vancouver
School:
Object-Oriented_Programming
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades

Students also viewed documents