Dynamic Memory Allocation in C++
An Introduction
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Motivation
- An Observation:
- We often don't know how much space we will need to
store things at "compile time"
-
Dynamic memory allocation is the
allocation of memory at "run time"
- Past Experience:
- You've seen some of this before in Java, since all
objects use memory that is allocated at "run time"
Diferences between Static and Dynamic Memory Allocation
- Dynamically allocated memory is kept on the
memory heap (also known as the free store)
- Dynamically allocated memory can't have a "name" it must
be referred to
- Declarations are used to statically allocate memory, the
new
operator is used to dynamically allocate
memory
Pointing to Memory Allocated at Run Time
- An Example:
- Illustration:
Pointing to Memory Allocated at Run Time (cont.)
- An Example:
- Illustration:
Using Memory Allocated at Run Time
- An Example:
- Illustration:
Run Time Allocation of Arrays
- An Example:
- Illustration:
Run Time Allocation of Arrays (cont.)
- An Example:
- Illustration:
Run Time Allocation of Arrays (cont.)
- An Example:
- Illustration:
Run Time Allocation of Arrays (cont.)
- An Observation:
-
*a
and a[0]
are
syntactically different ways to refer to the same value
- An Explanation:
- The
[]
operator performs pointer arithemtic
and de-references the resulting pointer.
Returning Memory to the Heap
- How Big is the Heap?
- It can only contain as much physical memory as you have
installed or as much virtual memory as your
operating system can make available (if it supports
virtual memory)
- Running Out of Memory:
- Most applications request memory from the heap
when they are running
- It is possible to run out of memory (you may even have
gotten a message like "Running Low On Virtual Memory")
- So, it is important to return memory to the heap
when you no longer need it
Returning Memory to the Heap (cont.)
- The Opposite of
new
:
- In C++: The
delete
operator in C++
- In Java: The JVM has a garbage collector
that returns unused memory
- An Example:
- Illustration:
Returning Memory to the Heap (cont.)
-
Dangling Pointers:
- The
delete
operator does not delete the
pointer, it takes the memory being pointed to
and returns it to the heap
- It does not even change the contents of the pointer
- Since the memory being pointed to is no longer available
(and may even be given to another application),
such a pointer is said to be dangling
- An Example:
- Illustration:
Returning Memory to the Heap (cont.)
- Remember:
- Return memory to the heap before undangling
the pointer
- What's Wrong with the Following:
Returning Memory to the Heap (cont.)
- What About Arrays?
- You want to return all of the memory to the heap
- So, a different form of the
delete
operator is needed
- Also, the memory allocator must keep track of the size
of the array
- An Example:
- Illustration:
Returning Memory to the Heap (cont.)
- An Example:
- Illustration:
Memory Leaks
- An Explanation:
- Memory leaks when it is allocated from the heap
using the
new
operator but not returned to
the heap using the delete
operator
- An Example:
- Illustration:
Memory Leaks (cont.)
- An Example:
- Illustration:
Memory Leaks (cont.)
- An Example:
- Illustration:
Memory Leaks (cont.)
- An Example:
- Illustration:
Memory Leaks (cont.)
- An Example:
- Illustration:
Dynamic Allocation of Objects
A Simple Class to Get Us Started
cppexamples\memory\Weight.h
cppexamples\memory\Weight.cpp
Dynamic Allocation of Objects (cont.)
- An Observation:
- We can dynamically allocate memory for objects
- An Example:
- Illustration:
Dynamic Allocation of Objects (cont.)
- An Example:
- Illustration:
Dynamic Allocation of Objects (cont.)
- De-referencing Pointers to Objects:
- Works the same as with other pointers
- An Example:
- A "Shorthand" Operator:
-
w->pounds
- Inside of a class you can use
this->
Dynamic Allocation of Objects (cont.)
A More Complicated Class
cppexamples\memory\Person.h
Dynamic Allocation of Objects (cont.)
A More Complicated Class (cont.)
cppexamples\memory\Person.cpp
Dynamic Allocation of Objects (cont.)
- An Example:
- Illustration:
Dynamic Allocation of Objects (cont.)
- An Example:
- Illustration:
Dynamic Allocation of Objects (cont.)
- An Example:
- Illustration:
Dynamic Allocation of Objects (cont.)
- An Example:
- Illustration:
Dynamic Allocation of Objects (cont.)
- An Example:
- Illustration:
Dynamic Allocation of Objects (cont.)
- An Example:
- Illustration:
Dynamic Allocation of Objects (cont.)
- Returning Memory to the Heap:
- The memory manager doesn't know how many times the
object might have requested memory from the heap in its
various methods
- The object must keep track of this itself
- Destructor:
- A method with the job of returning all "supplemental"
memory used by an object
- Has the same name as the class but preceeded by
a tilde (i.e., the
~
character)
- Whenever the
delete
operator is applied to
a pointer to an object the object's destructor is
called
Dynamic Allocation of C Strings
- Recall:
-
Strings (in C) are just arrays of characters terminated
with a '\0'
- An Example:
Dynamic Allocation of C Strings (cont.)
- A Static Array of Dynamically Allocated Strings:
-
char *names[4];
int nameLength;
nameLength = 80;
for (int i=0; i<4; i++) names[i] = new char[nameLength];
strcpy(names[0],"Alice");
strcpy(names[1],"Bob");
strcpy(names[2],"Carol");
strcpy(names[3],"Dan");
Dynamic Allocation of Arrays of Objects
- An Example:
- Interpretation:
-
w
is a pointer to the array
-
Each element of
w
is a Weight
object (not a reference to a Weight
)
-
Each
Weight
is constructed using the
default constructor
An Aside on main
- So Far:
- Our main function has had no parameters
- Now:
- We can discuss a version of
main
that is passed command-line arguments
-
int main(int argc, const char *argv[])
There's Always More to Learn