lec16-dyn_mem
lec16-dyn_mem
Fundamental Computing
with C++
Dynamic Memory Management
Fall, 2024
Outline
• Dynamic Memory Management
• Allocation
• Deallocation
2
Dynamic Memory Management
• Manage memory usage at runtime
3
Memory Allocation: Operator new
int *p; p ?
• p is a pointer variable
• It is automatically allocated a memory space for
storing an address
4
Memory Allocation: Operator new
int *p; p ? ?
p = new int;
5
Memory Allocation: Operator new
int *p; p ? ?
p = new int;
6
Memory Allocation: Operator new
int *p; p 10
?
p = new int;
*p = 10;
7
Losing Allocated Space Bye bye!
int *p; p 10
p = new int;
*p = 10;
p = new int; ?
• The allocated space has no corresponding name in the
program
9
Memory Deallocation: Operator
delete
• For freeing up the previously allocated memory
space
• E.g.: int *p;
p = new int;
… p 10
delete p;
1 int *ptr;
2 … delete will do nothing
3 ptr = NULL; if pointer is NULL
4 delete ptr; // Safe, but redundant 15
Pitfalls in Using delete
1 int *ptr = new int; Dangerous to use a released space
2
3 delete ptr;
because it can be reallocated to
4 *ptr = 10; // Runtime error somebody else already
16
Using new and delete with 1-D
Arrays
1 int *a, i, n;
2 cin >> n; // Let's say, enter 100000000
3 a = new int[n];
4 // a is pointing to an array of 100,000,000 int's (~381MB)
5
6 for (i = 0; i < n; i++) A dynamically allocated
7 a[i] = …; array whose size is
8 … determined at runtime
9 // Free up ALL the space occupied by a
10 delete [] a;
foo
?
?
foo[1]
?
⋮ 19
(Optional)
Dynamic 2-D Array: Allocation
1 int **foo;
2 int row = 10, col = 4, i, j;
3
4 foo = new int *[row]; // Each element is an int-pointer
5
6 // Allocate a 1-D array for each row
7 for (i = 0; i < row; i++)
8 foo[i] = new int[col];
9 // Now foo can be used as a row-by-col 2-D array of int
10 for (i = 0; i < row; i++)
11 for (j = 0; j < col; j++)
12 foo[i][j] = …;
foo[1][2]
foo
? ? ? ? ?
?
foo[1] ? ? ? ?
?
⋮ ? ? ? ? 20
(Optional)
Dynamic 2-D Array: Deallocation
1 int **foo;
2 int row = 10, col = 4, i, j;
3
4 foo = new int *[row]; // Each element is an int-pointer
5 // Allocate a 1-D array for each row
6 for (i = 0; i < row; i++)
7 foo[i] = new int[col];
8 …
9 // Free the space of each row first
10 for (i = 0; i < row; i++)
11 delete [] foo[i];
12 delete [] foo; // Finally, free the array of pointers
foo
? ? ? ?
? ? ? ?
⋮ ? ? ? ? 21
(Optional)
foo foo[1][2]
0
1
2
⋮ ⋮
9 int **foo, *tmp;
foo = new int *[row];
Allocate a 1-D array (tmp) big enough tmp = new int[row * col];
for all elements in the 2-D array and for (i = 0; i < row; i++)
then share the space among all rows foo[i] = &tmp[col * i];23
(Optional)
datePtr->setMonth(12);
datePtr->setDay(25);
Destructor
class GradeBook { GradeBook.h
public:
…
~GradeBook(); // Destructor
private:
string courseName;
int n;
int *grades; // Dynamically allocated array
};
• Destructor name: “~” + class name
• No return type; no parameters
• At most one destructor per class
• Used for “clean up” purpose
• E.g., for freeing dynamically allocated data members 26
(Optional)
Destructor
… GradeBook.cpp
// Constructor
GradeBook::GradeBook(…) {
A GradeBook object contains
grades = new int[n];
… a dynamically allocated array
} data member grades
// Destructor
GradeBook::~GradeBook() {
delete [] grades;
} Need to free the allocated space
of the array in the destructor
27
(Optional)
Destructor
… GradeBook.cpp
int main() {
…
GradeBook *book = new GradeBook(…);
…
book->processGrades();
29