L2: Pointers
Pointers.
Dynamic Memory Management.
SFDV4001 / L2 - Pointers / 1
Pointers
Assign a pointer a type.int *ptr;
Assign a pointer the address of a variable. int a = 0; ptr = &a;
Use a pointer to assign a value (dereferencing).*ptr = 9; // the value of a is now = 9.
Pointer operators are *, &, >
SFDV4001 / L2 - Pointers / 2
1: void foo(int v) 2: {3: v = 2000;4: }5:6: void bar(int &v)7: {8: v = 3000;9: }10:11: void zee(int *v)12: {13: *v = 4000;14: foo(*v);15: }
17: int main() {18: int a=1;19: int b=2;20: int c[3]={3,4,5};21: int *d = &a;22: int &e = a;23: 24: a = b+c[0];25: b = a;26: c[1] = *d;27: *d = 7;28: e = 8;29: 30: foo(a);31: bar(b);32: zee(d);33: }
Pointers
SFDV4001 / L2 - Pointers / 3
Execution point: Line 23
Variable Name Variable Type
a int 0 1b int 4 2
c[0] int 8 3c[1] int 12 4c[2] int 16 5d pointer to int 20 0e pointer to int 24 0
Stack Location in
Memory
Stack Contents
SFDV4001 / L2 - Pointers / 4
Variable Name Variable Type
a int 0 5b int 4 2
c[0] int 8 3c[1] int 12 4c[2] int 16 5d pointer to int 20 0e pointer to int 24 0
Stack Location in
Memory
Stack Contents
Execution point: Line 24: a = b+c[0]
SFDV4001 / L2 - Pointers / 5
Variable Name Variable Type
a int 0 5b int 4 5
c[0] int 8 3c[1] int 12 4c[2] int 16 5d pointer to int 20 0e pointer to int 24 0
Stack Location in
Memory
Stack Contents
Execution point: Line 25: b = a;
SFDV4001 / L2 - Pointers / 6
Variable Name Variable Type
a int 0 5b int 4 5
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0
Stack Location in
Memory
Stack Contents
Execution point: Line 26: c[1] = *d;
SFDV4001 / L2 - Pointers / 7
Variable Name Variable Type
a int 0 7b int 4 5
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0
Stack Location in
Memory
Stack Contents
Execution point: Line 27: *d = 7;
SFDV4001 / L2 - Pointers / 8
Variable Name Variable Type
a int 0 8b int 4 5
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0
Stack Location in
Memory
Stack Contents
Execution point: Line 28: e = 8;
SFDV4001 / L2 - Pointers / 9
Variable Name Variable Type
a int 0 8b int 4 5
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v int 28 8
Stack Location in
Memory
Stack Contents
Execution point: Line 30: foo(a); > Line 2
SFDV4001 / L2 - Pointers / 10
Variable Name Variable Type
a int 0 8b int 4 5
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v int 28 2000
Stack Location in
Memory
Stack Contents
Execution point: Line 3: v = 2000;
SFDV4001 / L2 - Pointers / 11
Variable Name Variable Type
a int 0 8b int 4 5
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v pointer to int 28 4
Stack Location in
Memory
Stack Contents
Execution point: Line 31: bar(b); > Line 7
SFDV4001 / L2 - Pointers / 12
Variable Name Variable Type
a int 0 8b int 4 3000
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v pointer to int 28 4
Stack Location in
Memory
Stack Contents
Execution point: Line 8: v = 3000;
SFDV4001 / L2 - Pointers / 13
Variable Name Variable Type
a int 0 8b int 4 3000
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v pointer to int 28 0
Stack Location in
Memory
Stack Contents
Execution point: Line 32: zee(d); > Line 12
SFDV4001 / L2 - Pointers / 14
Variable Name Variable Type
a int 0 4000b int 4 3000
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v pointer to int 28 0
Stack Location in
Memory
Stack Contents
Execution point: Line 13: *v = 4000;
SFDV4001 / L2 - Pointers / 15
Variable Name Variable Type
a int 0 4000b int 4 3000
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v pointer to int 28 0v int 32 4000
Stack Location in
Memory
Stack Contents
Execution point: Line 14: foo(*v); > Line 2
SFDV4001 / L2 - Pointers / 16
Variable Name Variable Type
a int 0 4000b int 4 3000
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v pointer to int 28 0v int 32 2000
Stack Location in
Memory
Stack Contents
Execution point: Line 3: v = 2000;
SFDV4001 / L2 - Pointers / 17
Variable Name Variable Type
a int 0 4000b int 4 3000
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0v pointer to int 28 0
Stack Location in
Memory
Stack Contents
Execution point: Line 15: };
SFDV4001 / L2 - Pointers / 18
Variable Name Variable Type
a int 0 4000b int 4 3000
c[0] int 8 3c[1] int 12 5c[2] int 16 5d pointer to int 20 0e pointer to int 24 0
Stack Location in
Memory
Stack Contents
Execution point: Line 33: }
SFDV4001 / L2 - Pointers / 19
Passing Argument
struct IntArray {
int contents[500];
};
int sum(IntArray a); //pass struct by value.
int sum(IntArray *a); //pass struct by ref. with pointer
int sum(IntArray &a); //pass struct by reference
int sum(const IntArray &a); //pass const struct. by reference
SFDV4001 / L2 - Pointers / 20
C++ has both statically allocated and dynamically allocated memory (like C, unlike Java).But you should never have to use malloc/free in a C++ program.Always use new and delete.
• Is not garbage collected unless 3rd party libraries are used.
Dynamic Memory Management
SFDV4001 / L2 - Pointers / 21
12:13: a = new int;14: b = new int;15: a = b;16: delete a;17: delete b;18: }
1 : int main() {2 : int *a = new int;3 : *a = 3;4 : delete a;5 : 6 : a = new int;7 : *a = 4;8 : int *b;9 : b = a;10: delete a;11: delete b;
Dynamic Memory Management
SFDV4001 / L2 - Pointers / 22
Variable Name Variable Type
a ptr to int 0 undefined
Type Heap Location Heap Contents
Stack Location in Memory
Stack Contents
Execution point: Line 1: int main() { WRONG ?
SFDV4001 / L2 - Pointers / 23
Variable Name Variable Type
a ptr to int 0 2000
Type Heap Location Heap ContentsInt 2000 Undefined
Stack Location in Memory
Stack Contents
Execution point: Line 2: int *a = new int;
SFDV4001 / L2 - Pointers / 24
Variable Name Variable Type
a ptr to int 0 2000
Type Heap Location Heap ContentsInt 2000 3
Stack Location in Memory
Stack Contents
Execution point: Line 3: *a = 3;
SFDV4001 / L2 - Pointers / 25
Variable Name Variable Type
a ptr to int 0 2000
Type Heap Location Heap Contents
Stack Location in Memory
Stack Contents
Execution point: Line 4: delete a;
SFDV4001 / L2 - Pointers / 26
Variable Name Variable Type
a ptr to int 0 3000
Type Heap Location Heap ContentsInt 3000 Undefined
Stack Location in Memory
Stack Contents
Execution point: Line 6: a = new int;
SFDV4001 / L2 - Pointers / 27
Execution point: Line 7: *a = 4;
Variable Name Variable Type
a ptr to int 0 3000
Type Heap Location Heap ContentsInt 3000 4
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 28
Execution point: Line 8: int *b;
Variable Name Variable Type
a ptr to int 0 3000b ptr to int 4 undefined
Type Heap Location Heap ContentsInt 3000 4
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 29
Execution point: Line 9: b = a;
Variable Name Variable Type
a ptr to int 0 3000b ptr to int 4 3000
Type Heap Location Heap ContentsInt 3000 4
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 30
Execution point: Line 10: delete a;
Variable Name Variable Type
a ptr to int 0 3000b ptr to int 4 3000
Type Heap Location Heap Contents
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 31
Execution point: Line 11: delete b;
Variable Name Variable Type
a ptr to int 0 3000b ptr to int 4 3000
Type Heap Location Heap Contents
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 32
Execution point: Line 13: a = new int;
Variable Name Variable Type
a ptr to int 0 3456b ptr to int 4 3000
Type Heap Location Heap Contentsint 3456 undefined
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 33
Execution point: Line 14: b = new int;
Variable Name Variable Type
a ptr to int 0 3456b ptr to int 4 5678
Type Heap Location Heap Contentsint 3456 undefinedint 5678 undefined
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 34
Execution point: Line 15: a = b;
Variable Name Variable Type
a ptr to int 0 5678b ptr to int 4 5678
Type Heap Location Heap Contentsint 3456 undefinedint 5678 undefined
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 35
Execution point: Line 16: delete a;
Variable Name Variable Type
a ptr to int 0 5678b ptr to int 4 5678
Type Heap Location Heap Contentsint 3456 undefined
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 36
Execution point: Line 17: delete b;
Variable Name Variable Type
a ptr to int 0 5678b ptr to int 4 5678
Type Heap Location Heap Contentsint 3456 undefined
Stack Location in Memory
Stack Contents
SFDV4001 / L2 - Pointers / 37