Dynamic Memory Allocation
Dynamic Memory Allocation
Pointer Variables
a pointer is a variable which holds the address of something else
a Zen saying
"lifetime" of a variable
is a run-time concept period of time during which a variable has memory space associated with it
begins when space is allocated ends when space is de-allocated
heap data
Heap Variables
are accessed indirectly via a pointer variable memory space is explicitly allocated at run-time (using new) space is allocated from an area of run-time memory known as the heap in C++ space must be explicitly returned (using delete) to avoid memory leak
C++ programmers are responsible for memory management
some examples
int * intPointer;
Time * timePointer;
intPointer ? timePointer ?
Dereferencing
heap variables do not have a name of their own
are anonymous variables
what happens?
intPointer = 36; *intPointer = 36; (*intPointer)++; cout << *intPointer; intPointer = null; intPointer
*intPointer
example
float * fPointer = new float; cin >> (*fPointer); -----delete fPointer;
Pointers - Summary
a pointer variable holds a memory address and can be used to create and access dynamic variables dynamic (heap) variables are explicitly created at run-time (using new) memory for dynamic variables is allocated in an area of memory called the heap space used for dynamic variables needs to be freed (using delete) to avoid memory leaks
45
1
15
2
--------------------
36
30
entry[0] = = *entry
float * dynArray; // declare pointer only dynArray = new float [max]; // max is a variable // use of dynamic array the same as use of an automatic array delete [ ] dynArray; // free heap space
Stack class
data members needed?
SE * myArray; int myTop; int myCapacity;
someStack
Constructor
must allocate the dynamic array needs a parameter to know how big an array to allocate
Stack::Stack (int size) { myCapacity = size; myArray = new SE[myCapacity]; assert (myArray != null); myTop = -1; }
myArray
myTop myCapacity
a Stack object
void func ( ) { Stack someStack (5); ----------}
someStack
myArray
myTop
-1
myCapacity 5
when func returns space for its activation record on the run-time stack is reclaimed
destructor
needed in order to prevent "memory leaks"
heap memory space which is no longer accessible but has not been returned (using delete)
destructor is a method that is called implicitly when the function in which an object was declared returns
compiler provides a "default destructor" nothing more is needed unless the object makes use of heap memory space (allocates space using new)
copy constructor
needed in order to make a deep rather than a shallow copy of an object when is a copy of an object made?
a value parameter requires a copy of the argument
void someFunc (Stack s) { ---- }
a shallow copy
compiler provides a default copy constructor
it makes a shallow copy when a copy is needed nothing more is needed unless the object makes use of heap memory space (allocates space using new) Stack a (8); Stack b (a); a
myArray myTop
b
myArray myTop
myCapacity 8
myCapacity 8
a deep copy
the copy must have its own heap memory space, the contents of which is the same as the object it is a copy of
Stack a (8); Stack b (a); a
myArray myTop
b
myArray myTop
myCapacity 8
myCapacity 8
a copy constructor
myClass (const myClass & sourceObj); myClass::myClass (const myClass & sourceObj) { //copy all non-heap data members using = //allocate heap space required by the copy //copy data stored in sourceObj's heap //memory to new object's heap memory }
makes a new deep copy of an existing object
Stack (const Stack & sourceStack); Stack::Stack (const Stack & sourceStack) { myCapacity = sourceStack.myCapacity; myTop = sourceStack.myTop; myArray = new SE [myCapacity]; assert (myArray != null); for (int pos = 0; pos < myTop; pos++) { myArray[pos] = sourceStack.myArray[pos]; } }
implementation
operator=
void operator= (const myClass & SourceObj); void myClass::operator= (const myClass & SourceObj); { //deallocate heap space used by left hand operand //copy all non-heap data members using = //allocate new heap space for the left hand operand //copy data stored in sourceObj's heap memory //to left hand operand's heap memory } same job that is done by the copy constructor
void operator= (const Stack & sourceStack); void Stack::operator= (const Stack & sourceStack) { if (this != &sourceStack) // check for self copy { delete [ ] myArray; // deallocate previous array // same code as in the copy constructor // to make a deep copy of sourceStack } }
implementation