workin’ with pointas

Post on 01-Jan-2016

25 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

1

Workin’ with PointasWorkin’ with Pointas

An exercise in destroying your computer

2

What is this?What is this?

• Your worst nightmare!• Comes from pointer misuse

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

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

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)

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

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;

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

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?

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

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)

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;

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

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”

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

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

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

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

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!

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

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

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

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

24

BSOD!BSOD!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

42

Allocating SpaceAllocating Space

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

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

top related