CS250 - Data Structures & Algorithms: Lecture 2: Review C++ (Pointers & Dynamic Memory Allocation)
CS250 - Data Structures & Algorithms: Lecture 2: Review C++ (Pointers & Dynamic Memory Allocation)
15/10/2020
Recap slide
▪ Data types:
► Primitive Data Types
- Bool, Int, float etc.
► User-Defined Data Types (UDTs)
- Aggregate data types e.g., structures, array-of-integers etc.
► Abstract Data Types (ADTs)
- Does not specify how the data type is implemented
- In an object-oriented language such as C++, an ADT and its
implementation together make up a class
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 2
Today‘s lecture
▪ Pointers
▪ Referencing vs pointers
▪ Arrays & dynamic memory allocation
▪ Stack vs heap
▪ The new operator & memory leaks
▪ Concept of shalow/deep copying
▪ Void pointer
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 3
Pointers
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 4
Pointers
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 5
Pointers
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 6
Pointers
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 7
Pointers
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 8
Pointers
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 9
Pointer example
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 10
Pointer example
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 11
Pointer example
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 12
Differences between references & pointers
▪ cout << n << ' ' << *p << ' ' << r << endl;
*p = 9;?
Output: 5 5 5 r = 10;?
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 13
Constant pointer vs pointer constant
#include <iostream>
void main() {
const SIZE = 5
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 16
String Literals
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 17
Pointers & Arrays
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 18
Pointers: function arguments
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 19
Pointers: function arguments
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 20
Today‘s lecture
▪ Pointers
▪ Referencing vs pointers
▪ Arrays & dynamic memory allocation
▪ Stack vs heap
▪ The new operator & memory leaks
▪ Concept of shalow/deep copying
▪ Void pointer
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 21
Dynamic memory allocation
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 22
Dynamic memory allocation
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 23
The new operator
int x = 10;
int *ptr;
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 24
Memory leaks – delete operator
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 25
Case of 1D array
int *arr;
arr = new int[SIZE]; // allocation
M. Shahzad: Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 26