workin’ with pointas

43
1 Workin’ with Pointas Workin’ with Pointas An exercise in destroying your computer

Upload: cheyenne-kirk

Post on 01-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

Workin’ with Pointas. An exercise in destroying your computer. What is this?. Your worst nightmare! Comes from pointer misuse. Let’s look at Memory! Blue is memory address, Black is value, Red is variable name. 1 -4717. 2 -901. 3 76. 4 -0. 5 98131. 6 -1038. 7 -554. 8 7462. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Workin’ with Pointas

1

Workin’ with PointasWorkin’ with Pointas

An exercise in destroying your computer

Page 2: Workin’ with Pointas

2

What is this?What is this?

• Your worst nightmare!• Comes from pointer misuse

Page 3: Workin’ with Pointas

3

Let’s look at Memory!Let’s look at Memory!Blue is memory address, Blue is memory address, Black is value, Black is value, Red is variable nameRed is variable name

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 -4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 4: Workin’ with Pointas

4

Declare an intDeclare an intint myInt;int myInt;

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt-4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 5: Workin’ with Pointas

5

What’ve we done?What’ve we done?

• By declaring the int, we’ve taken up just enough memory to hold an int

• We don’t know where in memory (the address) that it’s located

• Computer picks “at random”• What value is at that memory

location?• Can we print that value out?

– The value –4717 would print! (garbage)

Page 6: Workin’ with Pointas

6

Copy 42 into that Section of Copy 42 into that Section of MemoryMemory

myInt = 42;myInt = 42;1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt 26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

42

Page 7: Workin’ with Pointas

7

PointersPointers

• Allow us to get to the address of where information is located

• Similar to call forwarding– Ask the pointer where to go– Go there for the information

• To create a pointer, we use the *• Still follows format of <data type>

<name>;• Example:

int* ptr;

Page 8: Workin’ with Pointas

8

Declare an int pointerDeclare an int pointerint* ptr;int* ptr;

1 -4717

2-901

3 76

4-0

5 ptr98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 9: Workin’ with Pointas

9

Now what have we done?Now what have we done?

• Created a new variable that’s of type ptr to a int

• Notice that we haven’t initialized the pointer to “point” to myInt yet

• What if we print the pointer out?

Page 10: Workin’ with Pointas

10

cout << ptr;cout << ptr;(prints out value of ptr: 98131)(prints out value of ptr: 98131)

1 -4717

2-901

3 76

4-0

5 ptr98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 11: Workin’ with Pointas

11

ProblemProblem

• How do we get address of myInt so ptr can point to it?

• Remember, we can still access the value of myInt directlyint someInt = myInt;

• We really need the pointer to store the address of where myInt is located

• We do not need to store the value of myInt for the pointer (just the address)

Page 12: Workin’ with Pointas

12

The & operatorThe & operator

• Use the & operator to get the address of where the variable is in memory

• What would the following statement print to the screen?cout << &myInt << endl;

Page 13: Workin’ with Pointas

13

What would happen?What would happen?cout << &myInt;cout << &myInt;

1 -4717

2-901

3 76

4-0

5 ptr98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 14: Workin’ with Pointas

14

Getting the Pointer to PointGetting the Pointer to Point

• We now need “ptr” to point to myInt

• Code:ptr = &myInt;

ptr is a pointer,so it expects anaddress to be assigned to it

Here, we get the address of wheremyInt is stored in memory and copythat value into “ptr”

Page 15: Workin’ with Pointas

15

BeforeBefore

1 -4717

2-901

3 76

4-0

5 ptr98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 16: Workin’ with Pointas

16

AfterAfterptr = &myInt;ptr = &myInt;

1 -4717

2-901

3 76

4-0

5 ptr25

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 17: Workin’ with Pointas

17

What would this do?What would this do?ptr = myInt;ptr = myInt;

1 -4717

2-901

3 76

4-0

5 ptr98186

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 18: Workin’ with Pointas

18

Oh no!Oh no!ptr = myInt;ptr = myInt;

1 -4717

2-901

3 76

4-0

5 ptr42

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 19: Workin’ with Pointas

19

Tricky Screens of Death!Tricky Screens of Death!

• Last thing to learn is how to “dereference” pointer

• This means “how to follow the pointer”

• Unfortunately, we use the * operator as well

• Example:cout << *ptr << endl; //Follow wherever ptr is pointing to and print that value out!

Page 20: Workin’ with Pointas

20

Follow the Pointer and Print Follow the Pointer and Print it Outit Out

cout << *ptr << endl;cout << *ptr << endl;1 -4717

2-901

3 76

4-0

5 ptr25

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 myInt42

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 21: Workin’ with Pointas

21

Another ExampleAnother ExampleBlue is memory address, Blue is memory address, Black is value, Black is value, Red is variable nameRed is variable name

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 -4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 22: Workin’ with Pointas

22

Declare a PointerDeclare a Pointerint *ptr;int *ptr;

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 ptr -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 -4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 23: Workin’ with Pointas

23

What would happen?What would happen?cout << *ptr << endl;cout << *ptr << endl;

1 -4717

2-901

3 76

4-0

5 98131

6 -1038

7 -554

8 7462

9 312

10 ptr -6

11 3619

12 -4717

13 60981

14 4148

15 86851

16 -5155

17 95151

18 -47

19 2251

20 0

21 -78781

22 -901

23-6

24 6720

25 -4717

26 -19

2721511

28 -9

29 17

30 -6561

31 -651

32 9

33 761

34 -896761

35 7851

36 -6

37 9996

38 674547

39 -6868

40 -1

41 5431

42 -4717

Page 24: Workin’ with Pointas

24

BSOD!BSOD!

Page 25: Workin’ with Pointas

25

• Because parameter passing only passes a copy so the function can’t change main’s variables!void cannotChange (int x) {

x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Why do I need Pointers?Why do I need Pointers?

0 1 2

3 4 5

6 7 8

-2 91 571

-2991 0 -33

41 61 -1

Page 26: Workin’ with Pointas

26

• Because parameter passing only passes a copy so the function can’t change main’s variables!void cannotChange (int x) {

x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Declare myIntDeclare myInt

0 1 2

3 4 5

6 7 8

-2 91 571

-2991 0 -33

41 17 -1

myInt

Page 27: Workin’ with Pointas

27

• Because parameter passing only passes a copy so the function can’t change main’s variables!void cannotChange (int x) {

x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Call the functionCall the function

0 1 2

3 4 5

6 7 8

-2 91 571

-2991 0 -33

41 17 -1

myInt

Page 28: Workin’ with Pointas

28

• Because parameter passing only passes a copy so the function can’t change main’s variables!void cannotChange (int x) {

x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Here’s where the Copy is Here’s where the Copy is MadeMade

0 1 2

3 4 5

6 7 8

-2 17 571

-2991 0 -33

41 17 -1

myInt

x

Page 29: Workin’ with Pointas

29

• Because parameter passing only passes a copy so the function can’t change main’s variables!void cannotChange (int x) {

x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Changing Only Local CopyChanging Only Local Copy

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 17 -1

myInt

x

Page 30: Workin’ with Pointas

30

• Because parameter passing only passes a copy so the function can’t change main’s variables!void cannotChange (int x) {

x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Print Out Local Copy (6)Print Out Local Copy (6)

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 17 -1

myInt

x

Page 31: Workin’ with Pointas

31

• Because parameter passing only passes a copy so the function can’t change main’s variables!void cannotChange (int x) {

x = 6;cout << x << endl;

}void main ( ) {

int myInt = 17;cannotChange (myInt);cout << myInt << endl;

}

Return to Main (print 17)Return to Main (print 17)(x is gone and leaves garbage)(x is gone and leaves garbage)

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 17 -1

myInt

Page 32: Workin’ with Pointas

32

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Now with PointersNow with Pointers

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 412 -1

Page 33: Workin’ with Pointas

33

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Declare myIntDeclare myInt

0 1 2

3 4 5

6 7 8

-2 6 571

-2991 0 -33

41 17 -1

myInt

Page 34: Workin’ with Pointas

34

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Declare a Pointer to myIntDeclare a Pointer to myInt

0 1 2

3 4 5

6 7 8

7 6 571

-2991 0 -33

41 17 -1

myInt

ptr

Page 35: Workin’ with Pointas

35

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Pass a Copy of ptrPass a Copy of ptr

0 1 2

3 4 5

6 7 8

7 6 571

-2991 0 -33

41 17 -1

myInt

ptr

Page 36: Workin’ with Pointas

36

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Pass a Copy of ptrPass a Copy of ptr

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 17 -1

myInt

ptr x

Page 37: Workin’ with Pointas

37

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Change Whatever x is Change Whatever x is Pointing tooPointing too

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 17 -1

myInt

ptr x

Page 38: Workin’ with Pointas

38

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Change Whatever x is Change Whatever x is Pointing tooPointing too

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 6 -1

myInt

ptr x

Page 39: Workin’ with Pointas

39

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Follow x and Print it Out (6)Follow x and Print it Out (6)

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 6 -1

myInt

ptr x

Page 40: Workin’ with Pointas

40

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

See the Change in main (6 See the Change in main (6 also)also)

0 1 2

3 4 5

6 7 8

7 6 7

-2991 0 -33

41 6 -1

myInt

ptr x

Page 41: Workin’ with Pointas

41

void canChange (int* x) {*x = 6;cout << *x << endl;

}void main ( ) {

int myInt = 17;int* ptr = &myInt;canChange (ptr);cout << myInt << endl;

}

Interesting NoteInteresting Note

At this point,these two statements print out the same thing!

cout << *ptr << endl;cout << myInt << endl

So do these!

cout << ptr << endl;cout << &myInt << endl;

WHY?

Page 42: Workin’ with Pointas

42

Allocating SpaceAllocating Space

• new vs. malloc ( );• delete vs. free( );

Page 43: Workin’ with Pointas

43

SummarySummary

• To understand pointers, you need to understand memory

• The & is the secret to it all!• Create and dereference with *• Passing a pointer to a function can

make changes to the main