Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Workin’ with Pointas An exercise in destroying your computer."— Presentation transcript:

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

2 2 What is this? Your worst nightmare! Comes from pointer misuse

3 3 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 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 27 21511 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 41 5431 42 -4717

4 4 Declare an int 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 27 21511 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 41 5431 42 -4717

5 5 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 6 Copy 42 into that Section of Memory 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 myInt26 -19 27 21511 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 41 5431 42 -4717 42

7 7 Pointers 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 ; Example: int* ptr;

8 8 Declare an int pointer int* ptr; 1 -4717 2 -901 3 76 4 -0 5 ptr 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 42 26 -19 27 21511 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 41 5431 42 -4717

9 9 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 10 cout << ptr; (prints out value of ptr: 98131) 1 -4717 2 -901 3 76 4 -0 5 ptr 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 42 26 -19 27 21511 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 41 5431 42 -4717

11 11 Problem How do we get address of myInt so ptr can point to it? Remember, we can still access the value of myInt directly int 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 12 The & 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 13 What would happen? cout << &myInt; 1 -4717 2 -901 3 76 4 -0 5 ptr 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 42 26 -19 27 21511 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 41 5431 42 -4717

14 14 Getting the Pointer to Point We now need “ptr” to point to myInt Code: ptr = &myInt; ptr is a pointer, so it expects an address to be assigned to it Here, we get the address of where myInt is stored in memory and copy that value into “ptr”

15 15 Before 1 -4717 2 -901 3 76 4 -0 5 ptr 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 42 26 -19 27 21511 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 41 5431 42 -4717

16 16 After ptr = &myInt; 1 -4717 2 -901 3 76 4 -0 5 ptr 25 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 42 26 -19 27 21511 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 41 5431 42 -4717

17 17 What would this do? ptr = myInt; 1 -4717 2 -901 3 76 4 -0 5 ptr 98186 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 42 26 -19 27 21511 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 41 5431 42 -4717

18 18 Oh no! ptr = myInt; 1 -4717 2 -901 3 76 4 -0 5 ptr 42 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 42 26 -19 27 21511 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 41 5431 42 -4717

19 19 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 20 Follow the Pointer and Print it Out cout << *ptr << endl; 1 -4717 2 -901 3 76 4 -0 5 ptr 25 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 42 26 -19 27 21511 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 41 5431 42 -4717

21 21 Another Example 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 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 27 21511 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 41 5431 42 -4717

22 22 Declare a Pointer 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 27 21511 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 41 5431 42 -4717

23 23 What would happen? 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 27 21511 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 41 5431 42 -4717

24 24 BSOD!

25 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? 012 345 678 -291571 -29910-33 4161

26 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 myInt 012 345 678 -291571 -29910-33 4117 myInt

27 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 function 012 345 678 -291571 -29910-33 4117 myInt

28 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 Made 012 345 678 -217571 -29910-33 4117 myInt x

29 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 Copy 012 345 678 -26571 -29910-33 4117 myInt x

30 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) 012 345 678 -26571 -29910-33 4117 myInt x

31 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) (x is gone and leaves garbage) 012 345 678 -26571 -29910-33 4117 myInt

32 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 Pointers 012 345 678 -26571 -29910-33 41412

33 33 void canChange (int* x) { *x = 6; cout << *x << endl; } void main ( ) { int myInt = 17; int* ptr = &myInt; canChange (ptr); cout << myInt << endl; } Declare myInt 012 345 678 -26571 -29910-33 4117 myInt

34 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 myInt 012 345 678 76571 -29910-33 4117 myInt ptr

35 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 ptr 012 345 678 76571 -29910-33 4117 myInt ptr

36 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 ptr 012 345 678 767 -29910-33 4117 myInt ptr x

37 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 Pointing too 012 345 678 767 -29910-33 4117 myInt ptr x

38 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 Pointing too 012 345 678 767 -29910-33 416 myInt ptr x

39 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) 012 345 678 767 -29910-33 416 myInt ptr x

40 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 also) 012 345 678 767 -29910-33 416 myInt ptr x

41 41 void canChange (int* x) { *x = 6; cout << *x << endl; } void main ( ) { int myInt = 17; int* ptr = &myInt; canChange (ptr); cout << myInt << endl; } Interesting 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 42 Allocating Space new vs. malloc ( ); delete vs. free( );

43 43 Summary 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


Download ppt "1 Workin’ with Pointas An exercise in destroying your computer."

Similar presentations


Ads by Google