l2: pointerssfdv4001.wdfiles.com/local--files/lectures/l2_pointers.pdf · c[0] int 8 3 c[1] int 12...

37
L2: Pointers Pointers. Dynamic Memory Management. SFDV4001 / L2 - Pointers / 1

Upload: others

Post on 23-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

L2: Pointers

Pointers.

Dynamic Memory Management.

SFDV4001 / L2 - Pointers / 1

Page 2: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 3: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 4: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 5: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 6: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 7: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 8: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 9: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 10: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 11: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 12: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 13: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 14: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 15: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 16: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 17: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 18: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 19: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 20: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 21: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 22: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 23: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 24: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 25: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 26: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 27: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 28: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 29: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 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

Page 30: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 31: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 32: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 33: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 34: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 35: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 36: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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

Page 37: L2: Pointerssfdv4001.wdfiles.com/local--files/lectures/L2_Pointers.pdf · c[0] int 8 3 c[1] int 12 5 c[2] int 16 5 d pointer to int 20 0 e pointer to int 24 0 v pointer to int 28

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