2023 Slot03 Pointer
2023 Slot03 Pointer
Session 03 -
Pointer & Dynamic Memory
Instructor:
Dr. LE Thanh Tung
1 Pointer
2 Dynamic Memory: Pointer, Array and String
3 Pointer Exercises
▪ Misuses of pointers can lead to both bizarre effects and very subtle
errors
?
ptr1
dynamic variable
Dr. LE Thanh Tung CSC10002 – Programming Techniques Page 18
Pointer
▪ The diagram used is called a
▪ pointer diagram
▪ it helps to visualize what memory we have allocated and what our
pointers are referencing
▪ notice that the dynamic memory allocated is of size int in this case
and, its contents is uninitialized
▪ new is an operator and supplies back an address of the memory set
allocated
char_ptr
4 characters
Dr. LE Thanh Tung CSC10002 – Programming Techniques Page 28
Allocating Arrays
▪ How to use the dynamic array
→ in the exact same way we do for any array
char_ptr
4 characters
Dr. LE Thanh Tung CSC10002 – Programming Techniques Page 29
Allocating Arrays
▪ The only difference is when we are finally done with the array
▪ Deallocation for array: delete [] char_ptr;
char_ptr
4 characters
int array[3][2];
int (*p1)[2]; //define pointer of same type as array
p1 = array; //assign pointer to point to array
delete[] ps;
For example,
Input array: {1, 2, 3, 2, 2, 2, 3, 1, 5}, S = 6
Return list: { (0, 2), (3, 5), (5, 7), (7,8) },
subarr_size = 4
Dr. LE Thanh Tung CSC10002 – Programming Techniques Page 50
THANK YOU
for YOUR ATTENTION