Lecture - 2 - ReviewC++ - PointersAndDynamicMemoryAllocation - Ahsan
Lecture - 2 - ReviewC++ - PointersAndDynamicMemoryAllocation - Ahsan
Assistant Professor
Department Of Computing (DOC),
School of Electrical Engineering & Computer Science (SEECS),
National University of Sciences & Technology (NUST)
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
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
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 3
Pointers
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 4
Pointers
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 5
Pointers
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 6
Pointers
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 7
Pointers
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 8
Pointers
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 9
What is a pointer variable?
10
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Using a Pointer Variable
2000
int x;
x = 12; 12
x
int x; 2000
x = 12; 12
x
3000
int* ptr;
2000
ptr = &x;
ptr
cout << *ptr;
int x; 2000
x = 12; 12 5
x
3000
int* ptr;
2000
ptr = &x; ptr
4000
char ch;
ch = ‘A’; A Z
ch
ptr
15
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Reference Variables
int x = 5;
int &z = x; // z is another name for x
int &y ; //Error: reference must be initialized
cout << x << endl; -> prints 5
cout << z << endl; -> prints 5
z = 9; // same as x = 9;
17
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Reference Variables Example
#include <iostream.h>
void p_swap(int *a, int *b)
{
// Function prototypes
(required in C++) int temp;
temp = *a; (2)
*a = *b; (3)
void p_swap(int *, int *);
*b = temp;
void r_swap(int&, int&);
}
int main (void){
void r_swap(int &a, int &b)
int v = 5, x = 10; {
cout << v << x << endl; int temp;
p_swap(&v,&x); temp = a; (2)
cout << v << x << endl; a = b; (3)
r_swap(v,x); b = temp;
cout << v << x << endl; }
return 0;
18
}
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Pointer example
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 19
Pointer example
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 20
Differences between references & pointers
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 21
Differences between references & pointers
▪ cout << n << ' ' << *p << ' ' << r << endl;
*p = 9;?
Output: 5 5 5 r = 10;?
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 22
Constant pointer vs pointer constant
#include <iostream>
void main() {
const SIZE = 5
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 24
Take Home
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 25