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