AEP CS2 DynamicMemoryAllocation
AEP CS2 DynamicMemoryAllocation
• Pointers
– Powerful feature of the C++ language
– One of the most difficult to master
– Essential for construction of interesting data
structures
2
Addresses and Pointers
Output:
100 FFF4
56.47 FFF0
4
Pointer Variables
• The pointer data type
– A data type for containing an address rather than
a data value
– Integral, similar to int
– Size is the number of bytes in which the target
computer stores a memory address
– Provides indirect access to values
5
Declaration of Pointer Variables
• A pointer variable is declared by:
dataType *pointerVarName;
– The pointer variable pointerVarName is used to
point to a value of type dataType
– The * before the pointerVarName indicates that
this is a pointer variable, not a regular variable
– The * is not a part of the pointer variable name
6
Declaration of Pointer Variables
• Example
int *ptr1;
float *ptr2;
– ptr1 is a pointer to an int value i.e., it can have
the address of the memory location (or the first of
more than one memory locations) allocated to an
int value
– ptr2 is a pointer to a float value i.e., it can
have the address of the memory location (or the
first of more than one memory locations) allocated
to a float value
7
Declaration of Pointer Variables
• Whitespace doesn’t matter and each of the
following will declare ptr as a pointer (to a
float) variable and data as a float
variable
8
Assignment of Pointer Variables
A pointer variable has to be assigned a valid memory
address before it can be used in the program
Example:
float data = 50.8;
float *ptr;
ptr = &data;
This will assign the address of the memory location
allocated for the floating point variable data to the
pointer variable ptr. This is OK, since the variable
data has already been allocated some memory
space having a valid address
9
Assignment of Pointer Variables
FFF0
FFF1
float data = 50.8; FFF2
float *ptr; FFF3
ptr = &data; data FFF4 50.8
FFF5
FFF6
10
Assignment of Pointer Variables
ptr FFF0
FFF1
float data = 50.8; FFF2
float *ptr; FFF3
ptr = &data; data FFF4 50.8
FFF5
FFF6
11
Assignment of Pointer Variables
12
Assignment of Pointer Variables
13
Initializing pointers
14
The NULL pointer
16
Dereferencing
• Example:
float data = 50.8;
float *ptr;
ptr = &data;
cout << *ptr;
• Once the pointer variable ptr has been declared,
*ptr represents the value pointed to by ptr (or the
value located at the address specified by ptr) and
may be treated like any other variable of float type
17
Dereferencing
• The dereferencing operator * can also be used
in assignments.
*ptr = 200;
– Make sure that ptr has been properly initialized
18
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 50.8
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
19
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 50.8
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
FFF4 50.80 20
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 27.4
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
21
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 27.4
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
27.4 22
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 27.4
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
27.4 23
Operations on Pointer Variables
• Assignment – the value of one pointer variable can
be assigned to another pointer variable of the same
type
• Relational operations - two pointer variables of the
same type can be compared for equality, and so on
• Some limited arithmetic operations
– integer values can be added to and subtracted
from a pointer variable
– value of one pointer variable can be subtracted
from another pointer variable
24
Pointers to arrays
• A pointer variable can be used to access the elements
of an array of the same type.
int gradeList[8] = {92,85,75,88,79,54,34,96};
int *myGrades = gradeList;
cout << gradeList[1];
cout << *myGrades;
cout << *(myGrades + 2);
cout << myGrades[3];
• Note that the array name gradeList acts like the
pointer variable myGrades.
25
Dynamic Memory Allocation
26
Types of Program Data
27
Allocation of Memory
• Static Allocation: Allocation of memory space
at compile time.
28
Dynamic Memory Allocation Diagram
High-end
Run-time allocated
Stack
memory
Heap
Compile-time
static data
allocated
memory
Program
code
Low-end
29
Dynamic memory allocation
30
Dynamic Memory Allocation
31
Dynamic memory allocation
• Pointers need to be used for dynamic
allocation of memory
• Use the operator new to dynamically allocate
space
• Use the operator delete to later free this
space
32
The new operator
33
The delete operator
34
Example
0EC4
0EC5
0EC6
0EC7
35
Example (Cont ..)
0EC4
0EC5
0EC6
0EC7
36
Example (Cont ..)
0EC4 22
0EC5
0EC6
0EC7
37
Example (Cont ..)
0EC4 22
Output:
0EC5
22 0EC6
0EC7
38
Example (Cont ..)
0EC4
0EC5
0EC6
0EC7
39
Example (Cont ..)
0EC4
0EC5
0EC6
0EC7
40
Dynamic allocation and deallocation of
arrays
• Use the [IntExp] on the new statement to
create an array of objects instead of a single
instance.
• On the delete statement use [] to indicate
that an array of objects is to be deallocated.
41
Example of dynamic array allocation
delete [] grades;
grades = NULL;
42
Dynamic allocation of 2D arrays
43
Dynamic allocation of 2D arrays (Cont
..)
To allocate space for the 2D array with r rows and c
columns:
You first allocate the array of pointers which will
point to the arrays (rows)
matrix = new int*[r];
This creates space for r addresses; each being a
pointer to an int.
Then you need to allocate the space for the 1D arrays
themselves, each with a size of c
for(i=0; i<r; i++)
matrix[i] = new int[c];
44
Dynamic allocation of 2D arrays (Cont
..)
• The elements of the array matrix now can be
accessed by the matrix[i][j] notation
• Keep in mind, the entire array is not in contiguous
space (unlike a static 2D array)
• The elements of each row are in contiguous space, but
the rows themselves are not.
– matrix[i][j+1] is after matrix[i][j] in
memory, but matrix[i][0] may be before or
after matrix[i+1][0] in memory
45
Example
46
Passing pointers to a function
47
Pointers as arguments to
functions
48
Example of pointer arguments
49
Example of reference arguments
void Swap(int &a, int &b);
void main ()
{
int x, y;
cin >> x >> y;
cout << x << " " << y << endl;
Swap(x,y); // passes addresses of x and y implicitly
cout << x << " " << y << endl;
}
50
More example
void main ()
{
int r, s = 5, t = 6;
int *tp = &t;
r = MyFunction(tp,s);
r = MyFunction(&t,s);
r = MyFunction(&s,*tp);
}
51
Memory leaks and
Dangling Pointers
52
Memory leaks
• When you dynamically create objects, you can access
them through the pointer which is assigned by the
new operator
• Reassigning a pointer without deleting the memory it
pointed to previously is called a memory leak
• It results in loss of available memory space
53
Memory leak example
*ptr1 = 8;
ptr2
*ptr2 = 5;
5
ptr2 = ptr1;
ptr1
8
How to avoid?
ptr2
5
54
Inaccessible object
• An inaccessible object is an unnamed object
that was created by operator new and which a
programmer has left without a pointer to it.
• It is a logical error and causes memory leaks.
55
Dangling Pointer
56
Dangling Pointer example
ptr1
int *ptr1 = new int;
8
int *ptr2;
*ptr1 = 8; ptr2
ptr2 = ptr1;
delete ptr1;
ptr1
57
Pointers to objects
Pointers to objects
59
Pointers to objects (Cont..)
63
Dynamic Allocation of a Class Object
• Consider the Rational class defined before
Rational *rp;
int a, b;
cin >> a >> b;
rp = new Rational(a,b);
(*rp).Display(); // rp->Display();
delete rp;
rp = NULL;
64