Lesson 06 - 04. Pointers and Reference Variables - Explicit Address - 02
Lesson 06 - 04. Pointers and Reference Variables - Explicit Address - 02
Pointers and
Reference Variables - 02
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
Example 03:
void display(short* p, int size) {
for (int i = 0; i < size; i++) {
cout << p[i] << " ";
} Output
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
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
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
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
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
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
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
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
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
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
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
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.)