261102
Computer Programming
Lecture12:DynamicMemoryAllocation
Allocation of Memory
AllocationofMemory
Dynamic MemoryAllocation
Dynamic
Memory Allocation
StaticAllocation:Allocationofmemoryspace
atcompiletime.
t
il ti
Dynamicallocation isusefulwhen
DynamicAllocation:Allocationofmemory
space at run time
spaceatruntime.
arraysneedtobecreatedwhoseextentisnot
knownuntilruntime
complexstructuresofunknownsize and/orshape
need to be constructed as the program runs
needtobeconstructedastheprogramruns
objectsneedtobecreatedandtheconstructor
argumentsarenotknownuntilruntime
Dynamic MemoryAllocation
Dynamic
Memory Allocation
Pointers needtobeusedfordynamicallocation
ofmemory
Usetheoperatornew
Use the operator new todynamicallyallocate
to dynamically allocate
space
Usetheoperatordelete tolaterfreethis
space
Ifmemoryisavailable,thenew operatorallocates
memoryspacefortherequestedobject/array,and
f th
t d bj t/
d
returnsapointerto(addressof)thememory
allocated.
IfsufficientmemoryisNOT
If sufficient memory is NOT available,thenew
available the new
operatorreturnsNULL.
Thedynamicallyallocatedobject/arrayexistsuntil
the delete operatordestroysit.
thedelete
operator destroys it
Dynamic MemoryAllocation
Dynamic
Memory Allocation
&p fe38 p
=
=
=
=
=
0
0x2f7620
0x2f7620
0x2f7620
0
&p
&p
&p
p
&p
&p
=
=
=
=
=
0x22fe38
0x22fe38
0x22fe38
0x22fe38
0x22fe38
*p = 3111712
*p
p = 69
*p = 3111712
Dynamic MemoryAllocation
Dynamic
Memory Allocation
&p 22fe38 p
10
Dynamic MemoryAllocation
Dynamic
Memory Allocation
&p 22fe38 p
2f7620
newint int
????
2f7620
p
p
p
p
p
=
=
=
=
=
0
0x2f7620
0x2f7620
0x2f7620
0
&p
&p
&p
p
&p
&p
=
=
=
=
=
0x22fe38
0x22fe38
0x22fe38
0x22fe38
0x22fe38
2f7620
newint int
69
2f7620
p
p
p
p
p
*p = 3111712
*p
p = 69
*p = 3111712
=
=
=
=
=
0
0x2f7620
0x2f7620
0x2f7620
0
&p
&p
&p
p
&p
&p
=
=
=
=
=
0x22fe38
0x22fe38
0x22fe38
0x22fe38
0x22fe38
*p = 3111712
*p
p = 69
*p = 3111712
11
Dynamic MemoryAllocation
Dynamic
Memory Allocation
&p 22fe38 p
12
Dynamic MemoryAllocation
Dynamic
Memory Allocation
&p 22fe38 p
2f7620
Dangling
Dangling
Pointer
2f7620
2f7620
????
Freememory
space here
spacehere
p
p
p
p
p
=
=
=
=
=
0
0x2f7620
0x2f7620
0x2f7620
0
&p
&p
&p
p
&p
&p
=
=
=
=
=
0x22fe38
0x22fe38
0x22fe38
0x22fe38
0x22fe38
*p = 3111712
*p
p = 69
*p = 3111712
p
p
p
p
p
=
=
=
=
=
0
0x2f7620
0x2f7620
0x2f7620
0
&p
&p
&p
p
&p
&p
=
=
=
=
=
0x22fe38
0x22fe38
0x22fe38
0x22fe38
0x22fe38
*p = 3111712
*p
p = 69
*p = 3111712
????
13
Dynamic MemoryAllocation
Dynamic
Memory Allocation
14
Dynamic MemoryAllocation
Dynamic
Memory Allocation
Output
p = 0x577620
q = 0x577640
&p = 0x22fe38
&q = 0x22fe30
Output
*p = 69
*q = 55
p = 0x2f7620
q = 0x2f7620
*p = 55
&p = 0x22fe38
&q = 0x22fe30
*p = 69
*q = 55
15
16
(automaticstorage)
17
18
Thiscreatesspaceforr
This creates space for r addresses;eachbeinga
addresses; each being a
pointertoan int.
Thenyouneedtoallocatethespaceforthe1D
Then you need to allocate the space for the 1D
arraysthemselves,eachwithasizeofc
for(i=0;
f
(i 0 i<r;
i
i )
i++)
matrix[i] = new int[c];
20
21
22
DynamicAllocation
Equivalent to float c[N][M]
Equivalenttofloatc[N][M]
Deallocation of2DArray
23
24
new
new
float**
float*
c[0]
float*
c[1]
float*
c[2]
floatc[0][0]
floatc[0][1]
floatc[0][M]
Notcontiguous
N
t
ti
between2rows
new
float*
c[N]
floatc[1][0]
floatc[1][1]
new
floatc[N][0]
floatc[N][1]
floatc[N][M]
floatc[1][M]
5 5
0x2f765c
0x2f7b2c
0x2f7b4c
0x2f7b6c
0x2f7b8c
0x2f7660
0x2f7b30
0x2f7b50
0x2f7b70
0x2f7b90
25
26
Memory Leaks
MemoryLeaks
Whenyoudynamicallycreateobjects,youcanaccess
themthroughthepointerwhichisassignedbythe
new operator
Reassigningapointerwithoutdeletingthememory
Reassigning a pointer without deleting the memory it
it
pointedtopreviouslyiscalledamemoryleak
Itresultsinlossofavailablememoryspace
Input
p
size of y
your matrix:
Input row [1]: 1 2 3 4 5
Input row [2]: 6 7 8 9 10
Input row [3]: 4 5 7 8 3
Input row [4]: 1 1 2 2 2
Input row [5]: 1 4 7 5 5
0x22fd40 0x22fd44 0x22fd48
0x22fd54 0x22fd58 0x22fd5c
0x22fd68 0x22fd6c 0x22fd70
0x22fd7c 0x22fd80 0x22fd84
0x22fd90 0x22fd94 0x22fd98
5 5
0x22fd4c
0x22fd60
0x22fd74
0x22fd88
0x22fd9c
0x22fd50
0x22fd64
0x22fd78
0x22fd8c
0x22fda0
27
Memory Leaks
MemoryLeaks
int *ptr1 = new int;
int *ptr2 = new int;
*ptr1 = 8;
* t 2 = 5;
*ptr2
5
ptr2 = ptr1;
Inaccessible Object
ptr1
8
ptr2
5
ptr1
8
How to avoid?
28
ptr2
5
Aninaccessibleobjectisanunnamedobject
that was created by operator new andwhicha
thatwascreatedbyoperatornew
and which a
programmerhasleftwithoutapointertoit.
Itisalogicalerrorandcausesmemoryleaks.
29
Dangling Pointer
DanglingPointer
30
Dangling Pointer
DanglingPointer
Itisapointerthatpointstodynamicmemory
that has been deallocated
thathasbeendeallocated.
g
g gp
Theresultofdereferencingadanglingpointer
isunpredictable.
int *ptr1
ptr1 = new int;
int *ptr2;
p
= 8;
;
*ptr1
ptr2 = ptr1;
delete ptr1;
p
ptr1
8
ptr2
ptr1
How to avoid?
ptr2
31
Dangling Pointer
DanglingPointer
32
Dangling Pointer
DanglingPointer
AutomaticAllocation
AutomaticAllocation
int *
myPtr
int *
myPtr =22fdf4
Dangling
Pointer
&temp=22fdf4
int *
p=22fdf4
int
temp=69
int *
p=22fdf4
int
temp=69
address22fdf4
p = 0x22fdf4
myPtr = 0x22fdf4
*
*myPtr
= 0
p = 0x22fdf4
myPtr = 0x22fdf4
*
*myPtr
= 0
p andtemp aredestroyed
afterleavingfunction
33
Dangling Pointer
DanglingPointer
34
Dangling Pointer
DanglingPointer
DynamicAllocation
StaticAllocation
int *
myPtr =537620
int *
myPtr =4a7034
Dangling
Pointer
int *
p=537620
newint
69
int *
p=4a7034
address=537620
p = 0x537620
myPtr = 0x537620
*myPtr = 69
Beforedelete operator
int
temp=69
&temp=4a7034
p = 0x4a7034
myPtr = 0x4a7034
*myPtr = 69
p isdestroyedafterleaving
is destroyed after leaving
function
temp isnotdestroyed
is not destroyed
afterleavingfunction