Dynamic Memory PDF
Dynamic Memory PDF
Dynamic Memory PDF
int * myIntPtr;
myIntPtr = &myInt;
int i = 42;
int * p1, *p2; 42 int i
p1 = &i;
int *p2
int *p1
Before
Pointers and Dynamic Memory
Pointers can be set with the usual assign operator.
int i = 42;
int * p1, *p2; 42 int i
p1 = &i;
//--------------
p2 = p1;
int *p2
int *p1
After
Pointers and Dynamic Memory
Contents of pointer variables can be set with the
assign operator.
42 int i 17 int j
int i = 42;
int j = 17
int * p1, *p2;
p1 = &i;
int *p2
p2 = &j; int *p1
Before
Pointers and Dynamic Memory
Contents of pointers variables can be set with the assign
operator.
17 int i 17 int j
*p1 = *p2;
int * myIntPtr
myIntPtr = new int;
myIntPtr
Pointers and Dynamic Memory
Use pointer variable as before
Free Store (heap)
12 3
int * myIntPtr
myIntPtr myIntPtr = new int;
*myIntPtr = 123;
Pointers and Dynamic Memory
We can also allocate entire arrays with the new
operator. These are called dynamic arrays.
0 int * myIntPtr;
myIntPtr myIntPtr = new int[4];
1 3 2 5
2
3 myIntPtr[1] = 325;
set_value(main_ptr);
main_ptr 222
----------------------------------------
-
void set_value(int tempPtr[])
{
tempPtr[1] = 222; tempPtr
}
Pointers and Arrays as Const
Parameters
int *main_ptr;
main_ptr = new int[4];
set_value(main_ptr);
main_ptr
-----------------------------------------
void set_value(const int
tempPtr[])
{
tempPtr[1] = 222;
} Not allowed
Pointers and Dynamic Memory
Sometimes we may want a function to change a
pointer so that it points to a different location
inPtr myIntPtr;
42 42 Returned to
Delete p1;
free store
An inaccessable
int *p1 int *p2 int *p1 int *p2 object
p2 = p1;
Pointers and Dynamic Memory
An inaccessable memory location can also be
caused by inappropriate use of new operator.
42 42
An inaccessable
int *p1 object int *p1
p1 = new int;
Pointers and Dynamic Memory
Pointers also introduce potential problems with the
behavior of the automatic assignment operator, the
copy operation, and the comparison operation.
int * myIntPtr;
0 0 myIntPtr = new int[4];
myIntPtr 1 1
2 2 int * myIntPtr2;
3 3
myIntPtr2 = myIntPtr;
myIntPtr2
Not the default
Free Store (heap) behavior of ‘=‘
Pointers and Dynamic Memory
What we got!
int * myIntPtr;
0 myIntPtr = new int[4];
myIntPtr 1
2 int * myIntPtr2;
3
myIntPtr2 = myIntPtr;
myIntPtr2
DynamicClass newClass(oldClass);
…
};
Pointers and Dynamic Memory
DynamicClass::DynamicClass
(const DynamicClass& oldClass)
{
data = new Item[someSize];
for (int i=0; i < oldClass.currentSize; i++)
{
data[i] = oldClass[i];
}
}
Pointers and Dynamic Memory
0 A 0 A
1 B
oldClass
data 1 B
2 C 2 C
3 D 3 D
0 A A
0
1 B oldClass
data 1 B
2 C 2 C
3 D 3 D
4
5
6
Pointers and Dynamic Memory
delete [ ] oldClass;
0 A A
0
1 B oldClass
data 1 B
2 C 2 C
3 D 3 D
4
5 Return
6
memory
Pointers and Dynamic Memory
Class DynamicClass
{
public:
DynamicClass (const DynamicClass & oldClass);
void operator =(const DynamicClass & oldClass);
~DynamicClass ();
…
};
Pointers and Dynamic Memory
When we are finished using a class instance, we must free
any memory pointed to by variables in the class. This is
done automatically by invoking the code contained in the
class destructor. Failure to do so creates memory leaks.
DynamicClass::~DynamicClass() { }
Pointers and Dynamic Memory
The use of pointers and dynamic variables is an important
problem solving tool. It gives the class builder flexibility in
managing memory.