Lecture 04 Dynamic Memory Allocation
Lecture 04 Dynamic Memory Allocation
1 / 12
Review C++
2 / 12
Review C++
Pointer arithmetic
3 / 12
Review C++
Pointer arithmetic
1 unsigned int n;
2 std::cin >> n;
3 float x[n]; // will not work
6 / 12
Review C++
Examples:
dynamically allocates memory for a single int, and stores the
address in intPtr.
1 int * intPtr;
2 intPtr = new int;
1 int * intPtr;
2 intPtr = new int (6);
7 / 12
Review C++
8 / 12
Review C++
9 / 12
Review C++
Memory leaks
10 / 12
Review C++
Memory leaks
1 #include <stdlib.h>
2
3 int main ()
4 {
5 char * x = (char * ) malloc (100); // Memory Leak!
6
7 return 0;
8 }
11 / 12
Review C++
Reading Material
12 / 12