JMU
Dynamic Memory Allocation in C++
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
Diferences between Static and Dynamic Memory Allocation
Pointing to Memory Allocated at Run Time
Pointing to Memory Allocated at Run Time (cont.)
Using Memory Allocated at Run Time
Run Time Allocation of Arrays
Run Time Allocation of Arrays (cont.)
Run Time Allocation of Arrays (cont.)
Run Time Allocation of Arrays (cont.)
Returning Memory to the Heap
Returning Memory to the Heap (cont.)
Returning Memory to the Heap (cont.)
Returning Memory to the Heap (cont.)
Returning Memory to the Heap (cont.)
Returning Memory to the Heap (cont.)
Memory Leaks
Memory Leaks (cont.)
Memory Leaks (cont.)
Memory Leaks (cont.)
Memory Leaks (cont.)
Dynamic Allocation of Objects

A Simple Class to Get Us Started

cppexamples\memory\Weight.h
                
cppexamples\memory\Weight.cpp
                
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)

A More Complicated Class

cppexamples\memory\Person.h
                
cppexamples\memory\Person.cpp
                
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of Objects (cont.)
Dynamic Allocation of C Strings
Dynamic Allocation of C Strings (cont.)
Dynamic Allocation of Arrays of Objects
An Aside on main