0% found this document useful (0 votes)
5 views47 pages

Lesson 06 - 04. Pointers and Reference Variables - Explicit Address - 02

The document outlines a lesson on pointers and reference variables in C++, covering topics such as pointer arithmetic, pointers and arrays, and dynamic memory allocation. It includes examples demonstrating how to manipulate pointers and arrays, as well as how to allocate and deallocate memory dynamically using the 'new' and 'delete' operators. The instructor for the session is Nguyen Chien Thang from Duy Tan University.

Uploaded by

chi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views47 pages

Lesson 06 - 04. Pointers and Reference Variables - Explicit Address - 02

The document outlines a lesson on pointers and reference variables in C++, covering topics such as pointer arithmetic, pointers and arrays, and dynamic memory allocation. It includes examples demonstrating how to manipulate pointers and arrays, as well as how to allocate and deallocate memory dynamically using the 'new' and 'delete' operators. The instructor for the session is Nguyen Chien Thang from Duy Tan University.

Uploaded by

chi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

06.

Pointers and
Reference Variables - 02

Oritented Object Programming C++: Chapter 04

 Time: 120 Minutes


 Instructor: Thang, Nguyen Chien
 Email: [email protected]
 Phone: 0349 688 571
Previous lessons

1. Introduction C++
 C++ vs C
 Compilers, IDEs
2. C++ Language Basics
 Types: int, float, double
 Control Structures: if, for, while, switch-case…
3. Functions, Arrays, Strings and Parameter Passing
 void func(int); void func(int, int =
1);
 int arr[10], brr[2][3];
 #include <cstring>; char str[10];
 #include <string>; string s;
Duy Tan University
2
Previous lessons (cont.)
... ...
4. Pointers and Reference Variables 01 0x7
 int a = 1000000; 1
 cout << &a; 0x7
0
 int& ref = a; 0x6
0x65 p
 int* p = &a; f
 cout << *p; 0x6
e
 void iSwap(int& x, int& y); 0x6
 int& iMax(int& x, int& y); d
 void iSwap(int* x, int* y); 0x6
c
0x6
b
0x6
a
3 0x6 Duy Tan University
Contents

IV. Pointer Arithmetic


 +, -, ++, --
V. Pointers and Arrays
 Name of an array
 Operator []
VI. Dynamic Memory
 Operators new, delete, delete[]
 Heap and Stack

Duy Tan University


4
Discussion
... ...
 short is a number, we can do:
0x69
 short a = 10; 0x68
 a = a + 1; 0x67
0x66
10 a
 a = a - 1;
0x65
 A pointer is also a number: 0x64
 short* p = &a; 0x66 ... ...
 cout << p << endl;102
 cout << (long long)(p) << endl;
 Is it possible to do this?
p = p + 1; What is the
p = p - 2; Yes result?
Duy Tan University
5
IV. Pointer Arithmetic

 In C++, we can perform integer addition or


subtraction operations on pointers
 If p is a int pointer:
 p + 1 is the address of the next integer in memory after p
 p - 1 is the address of the previous integer before ptr
 Example 01: 0x66 ... ...
short a = 10; 0x68 0x69
short* p = &a; 0x64 0x68
0x67
cout << p << endl; 0x66
10 a
cout << (p + 1) << endl; 0x65
cout << (p - 1) << endl; 0x64
... ...
Duy Tan University
6
IV. Pointer Arithmetic (cont.)

 Example 02: ... ... arr


0x7
short arr[5] = { 0, 1, 2, 3, 4 };
1
short* p = &arr[2]; 0x7
cout << p << "_" << *p << endl; 0
4 [4]
cout << (p+1) << "_" << *(p+1) << 0x6
endl; f
0x6
cout << (p+2) << "_" << *(p+2) <<
e
endl; 3 [3]
0x6
cout << (p-1) << "_" << *(p-1) << d
endl; Output 0x6
cout 0x6b_2
<< (p-2) << "_" << *(p-2) << c
2 [2]
endl;0x6d_3 0x6
0x6f_4
b
0x69_1
0x67_0 0x6
a
7 1Duy Tan University
[1]
IV. Pointer Arithmetic (cont.)

 Example 02: Explain ... ... arr


0x7
short arr[5] = { 0, 1, 2, 3, 4 };
1
short* p = &arr[2]; 0x7
cout << p << "_" << *p << endl; 0
4 [4]
cout << (p+1) << "_" << *(p+1) << 0x6
endl; f
0x6
cout << (p+2) << "_" << *(p+2) <<
e
endl; 3 [3]
0x6
cout << (p-1) << "_" << *(p-1) << d
endl; Output 0x6
cout 0x6b_2
<< (p-2) << "_" << *(p-2) << c
2 [2]
endl;0x6d_3 0x6
0x6f_4
b
0x69_1
0x67_0 0x6
a
8 1Duy Tan University
[1]
IV. Pointer Arithmetic (cont.)

 Example 03: ... ... arr


0x7
short arr[5] = { 0, 1, 2, 3, 4 };
1
short* p = &arr[0]; 0x7
p = p + 3; 0
4 [4]
cout << p << "_" << *p << endl; 0x6
f
++p;
0x6
cout << p << "_" << *p << endl; e
cout << (&arr[0] + 3) << endl; 3 [3]
0x6
cout << *(&arr[0] + 3) << endl; d
0x6
Output c
0x6d_3 2 [2]
0x6
0x6f_4 b
0x6d
3 0x6
a
9 1Duy Tan University
[1]
IV. Pointer Arithmetic (cont.)

 Example 03: Explain ... ... arr


0x7
short arr[5] = { 0, 1, 2, 3, 4 };
1
short* p = &arr[0]; 0x7
p = p + 3; 0
4 [4]
cout << p << "_" << *p << endl; 0x6
f
++p;
0x6
cout << p << "_" << *p << endl; e
cout << (&arr[0] + 3) << endl; 3 [3]
0x6
cout << *(&arr[0] + 3) << endl; d
0x6
Output c
2 [2]
0x6d_3 0x6
0x6f_4 b
0x6d
3
0x6
a
10 1Duy Tan University
[1]
IV. Pointer Arithmetic (cont.)

 Example 04: ... ... arr


short arr[5] = { 9, 3, 6, 15, 10 }; 0x7
1
short* p1 = &arr[0]; 0x7
short* p2 = &arr[3]; 0
10 [4]
cout << (p2 - p1) << endl; 0x6
f
if (p2 > p1) {
0x6
cout << "p2 > p1"; e
} else if (p2 < p1) { 15 [3]
Output 0x6
cout << "p2 < p1"; 3 d
} else { p2 > p1 0x6
c
cout << "p2 == p1"; 6 [2]
0x6
} b
0x6
a
11 3Duy Tan University
[1]
Contents

IV. Pointer Arithmetic


 +, -, ++, --
V. Pointers and Arrays
 Name of an array
 Operator []
VI. Dynamic Memory
 Operators new, delete, delete[]
 Heap and Stack

Duy Tan University


12
IV. Pointers and Arrays

 An array's name is a constant pointer, pointing to the


first element (index 0) of the array.
 [] is an operator, with short arr[4];
 arr + i is same as &arr[i] ... ...
0x6f
 *(arr + i) same as arr[i] 0x6e *(arr+3
9 arr[3]
 Example 01: 0x6d )
short arr[4] = { 8, 2, 1, 9 }; 0x6c *(arr+2
1 arr[2]
0x6b )
cout << &arr[0] << endl;
Output 0x6a *(arr+1
cout << arr << endl; 2 arr[1]
0x69 )
cout << *arr << endl;
0x67
0x68
0x67 8 arr[0]
*arr
cout << *(arr + 2); 0x67
8
... ...
1
Duy Tan University
13
IV. Pointers and Arrays (cont.)

 Example 02: ... ... arr


0x7
short arr[5] = { 9, 3, 6, 15, 10
1
};
0x7
short* p = arr; 0
10 [4]
cout << *(p + 2) << endl; 0x6
cout << *(arr + 2) << endl; f
0x6
cout << p[2] << endl;
e
cout << arr[2] << endl; 15 [3]
Output 0x6
short b = 10; d
6 0x6
p = &b;
6 c
arr = &b; 6 [2]
6 0x6
6 b
0x6
a
14 3Duy Tan University
[1]
IV. Pointers and Arrays (cont.)

 Example 03:
void display(short* p, int size) {
for (int i = 0; i < size; i++) {
cout << p[i] << " ";
} Output

cout << endl; 9 3 6 15 10


}
int main() {
short arr[5] = {9, 3, 6, 15, 10};
display(arr, 5);
return 0;
}

Duy Tan University


15
Contents

IV. Pointer Arithmetic


 +, -, ++, --
V. Pointers and Arrays
 Name of an array
 Operator []
VI. Dynamic Memory
 Operators new, delete, delete[]
 Heap and Stack

Duy Tan University


16
Discussion

 Enter n (0 < n < 1000) and n integers for an array.


int main()
{
int n = 0; What is the
int arr[1000]; problem?
cout << "Enter n: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "arr[" << i << "]: ";
cin >> arr[i];
}
return 0;
}
Duy Tan University
17
VI. Dynamic Memory

 C++ supports three basic types of


Function
memory allocation: arguments Stack
 Automatic memory allocation Local variables
 Dynamic memory allocation
 Static memory allocation
 Both automatic and static new
delete Heap
allocation: delete[]
 The size of the variable/array is
fixed Static, global
Data
• int a = 10; variables
• int arr[10]; Code
 Memory allocation and deallocation
happens automatically 18 Duy Tan University
VI. Dynamic Memory (cont.)

 Allocate memory dynamically by using new operator:


Allocate an element:
• pointer = new type;
Allocate a sequence of elements:
• pointer = new type [number_of_elements];
 Example 01:
 int* p = new int;
 int* pArr = new int[5];
 Dellocate the memory by delete, delete[]
operators
 delete p; // Dellocate an element
 delete[] pArr;//Dellocate a sequence of
elements 19 Duy Tan University
VI. Dynamic Memory (cont.)
int*
 Example 02:
0x6a
int main() { int* int
p
int* p = new int; 0x8a 10
pArr 0x6a
*p = 10;
cout << *p << endl; int int int int int
int* pArr = new int[5]; 0 1 2 3 4
0x8a
for (int i = 0; i < 5; i++) 0x8e 0x93 0x97 0x9b
*(pArr + i) = i;
for (int i = 0; i < 5; i++)
Output
cout << pArr[i] << " ";
return 0; 10
01234
}

Duy Tan University


20
VI. Dynamic Memory (cont.)

 Example 03:
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
int g = 1;
int main() {
func(3);
func(2); Data
cout << g;
Code
} 21 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03:
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
int g = 1;
int main() {
func(3);
func(2); g 1 Data
cout << g;
Code
} 22 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03:
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
int g = 1;
int main() {
func(3);
func(2); g 1 Data
cout << g;
Code
} 23 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 3

void func(int ag) {


Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
int g = 1;
int main() {
func(3);
func(2); g 1 Data
cout << g;
Code
} 24 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 3
local 20
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
int g = 1;
int main() {
func(3);
func(2); g 1 Data
cout << g;
Code
} 25 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 3
local 20
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 Data
cout << g;
Code
} 26 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 3
local 20
void func(int ag) {
Stack
int local = 20; p 0x6a

int* p = new int;


int* pArr = new
int[ag];
static int a = 2;
} Heap
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 Data
cout << g;
Code
} 27 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 3
local 20
void func(int ag) {
Stack
int local = 20; p 0x6a

int* p = new int;


int* pArr = new
int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 Data
cout << g;
Code
} 28 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 3
local 20
void func(int ag) {
Stack
int local = 20; p 0x6a

int* p = new int; pArr 0x70

int* pArr = new


int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 Data
cout << g;
Code
} 29 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 3
local 20
void func(int ag) {
Stack
int local = 20; p 0x6a

int* p = new int; pArr 0x70

int* pArr = new


int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 30 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 3
local 20
void func(int ag) {
Stack
int local = 20; p 0x6a

int* p = new int; pArr 0x70

int* pArr = new


int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 31 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03:
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 32 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 2

void func(int ag) {


Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 33 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 2
local 20
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 34 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 2
local 20
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x7c 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 35 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 2
local 20
void func(int ag) {
Stack
int local = 20; p 0x7c

int* p = new int;


int* pArr = new
int[ag];
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x7c 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 36 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 2
local 20
void func(int ag) {
Stack
int local = 20; p 0x7c

int* p = new int;


int* pArr = new
int[ag];
0x82 0x86
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x7c 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 37 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 2
local 20
void func(int ag) {
Stack
int local = 20; p 0x7c

int* p = new int; pArr 0x82

int* pArr = new


int[ag];
0x82 0x86
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x7c 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 38 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03: ag 2
local 20
void func(int ag) {
Stack
int local = 20; p 0x7c

int* p = new int; pArr 0x82

int* pArr = new


int[ag];
0x82 0x86
static int a = 2;
} Heap
0x70 0x74 0x78
int g = 1;
int main() {
func(3); 0x7c 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 39 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 03:
void func(int ag) {
Stack
int local = 20;
int* p = new int;
int* pArr = new
int[ag];
0x82 0x86
static Memory
int a leak
= 2;
} Heap
0x70 0x74 0x78
int g = 1;
Output
int main() {
func(3);
1 0x7c 0x6a

func(2); g 1 a 2 Data
cout << g;
Code
} 40 Duy Tan University
VI. Dynamic Memory (cont.)

 Example 04: enter n integers and display the integers


int n; Output
int* pArr = nullptr; n: 3
cout << "n: "; cin >> n; arr[0]: 4
arr[1]: 9
pArr = new int[n]; arr[2]: 1
for (int i = 0; i < n; i++) { 491
cout << "arr[" << i << "]: ";
cin >> pArr[i];
}
for (int i = 0; i < n; i++)
cout << pArr[i] << " ";
delete[] pArr; // Delocate a sequence of
elements
Duy Tan University
41
VI. Dynamic Memory (cont.)

 Example 04: Solution 2


int* pN = new int; Output
int* pArr = nullptr; n: 3
cout << "n: "; cin >> *pN; arr[0]: 4
arr[1]: 9
pArr = new int[*pN]; arr[2]: 1
for (int i = 0; i < *pN; i++) { 491
cout << "arr[" << i << "]: ";
cin >> pArr[i];
4 9 1
}
Heap
for (int i = 0; i < *pN; i++) 3
cout << pArr[i] << " ";
delete[] pArr; // Delocate a sequence of
elements
delete pN; // Delocate an
42
element Duy Tan University
VI. Dynamic Memory (cont.)
Parameter Stack Heap
Variables Argument and local Use a pointer to hold the
variables memory address
Allocation When the variables Use new operator
declared
Deallocatio When the variables out of Use delete and delete[]
n scopes operators
Basic Memory is allocated in a Memory is allocated in
contiguous block any random order.
Access time Faster Slower

Duy Tan University


43
Contents

IV. Pointer Arithmetic


 +, -, ++, --
V. Pointers and Arrays
 Name of an array
 Operator []
VI. Dynamic Memory
 Operators new, delete, delete[]
 Heap and Stack

Duy Tan University


44
Summary

 Pointer Arithmetic: +, -, ++, --


 Pointer comparison: ==, >, >=, <, <=, !=
 An array's name is a constant pointer, pointing to the
first element (index 0) of the array
 When arr is a pointer
 *(arr + i) is same as arr[i]
 (arr + i) is same as &ar[i]
 Manage the dynamic memory by operators:
 new operator to allocate memory on the Heap
 delete and delete[] operators to deallocate the memory

Duy Tan University


45
Problems

1. Use pointer to write input(…) function to enter an


integer array.
2. Use pointer to write display(…) function to display
an integer array.
3. Write createArray(int n) function to create an
integer array with n element have random values,
the function return the array.
4. Use pointers and dynamic memory allocation to
write pGetLine() function to get a line from the
keyboard and return as it.

Duy Tan University


46
Problems (cont.)

5. Use pointers and dynamic memory allocation to


write pGetLines(int n) function to get n line from
the keyboard and return them.
6. Use pointers to write input(…) function to get
values for a matrix (2D array)
7. Use pointers to write display(…) function to display
values of a matrix (2D array)

Duy Tan University


47

You might also like