Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Working with Pointers An exercise in destroying your computer.

Similar presentations


Presentation on theme: "1 Working with Pointers An exercise in destroying your computer."— Presentation transcript:

1 1 Working with Pointers 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! Let’s look at Memory! Blue is memory address, Black is value 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 7Pointers 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 * 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 of type ptr that points to an 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; 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 Wrong Address 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 “de-reference” a 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 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 Blue Screen of Death!

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 Summary of Pointers To understand pointers, you need to understand memory The & is the secret to it all! Create and de-reference with * Passing a pointer to a function can make changes to the main

43 Lists, Linked Lists, Stacks, and Queues

44 A List is a collection of data elements in an array structure. From any given element in the array, the previous and next items can be obtained by moving to adjacent memory locations. A Linked List is a collection of data elements where the location of each element in memory is only recorded by a pointer from a previous element. The data elements may happen to be adjacent in memory, but you cannot rely on this. What the is a Linked List?

45 List : (Array of 5 elements : simple integers) Linked List : (5 elements - all simple integers) 275569510122 1016955527

46 –Are just a sequence of data linked together in memory. –Each element in the list is made up of data plus a pointer to the next element in the list. –Node is the common name used for this data plus the associated pointer to the next item in the list. –However, many kilo-bytes of data : names, addresses, student-ids, etc - could be stored at each Node. –A chain is only as strong as its weakest link. –Similarly, if something erased or corrupted a Node in the middle of the list, then Nodes after this will NOT be found ! What the is a Linked List?

47 Queues : FIFO Vs LIFO Queues can be of two main types : –FIFO - First In First Out The first item into the queue is the first to be processed or output. Example : Supermarket checkout queue. –LIFO queue - Last In First Out. The last item into the queue is the first one out. Example : Stack of paper - where you put paper on top of each other and can only get to a page in the stack by removing each page on top of it in turn.

48 Linked Lists - All are LIFO in this Lecture We are going to build all Linked Lists in this lecture on a LIFO queue, but we could have made them all FIFO instead. Exercise : As an exercise that will help you understand queues and Linked Lists, you can convert these examples to FIFO.

49 Building a Basic Linked List #include // for cin and cout. #include // for NULL. #include // getchar // Define the type of data to be stored at each node typedef float ListElement; struct Node { ListElement Value;// Data stored at this Node Node* Next;// Pointer to Next Node in the List };

50 Basic Linked List - only 4 Functions class Linked_List { private: Node *First; public: Linked_List(); // Constructor // Add Node into the start of a Linked List void Insert_Node (ListElement In_Value); };

51 Basic Linked List - Diagram Once we have setup the class member functions and created a main() program to use our Linked List class, our Linked List could be visualized as : Value 3 Next First NULL Value 1 Next Value 2 Next

52 Basic Linked List - Constructor Linked_List::Linked_List() // Constructor { // Create an empty list First = NULL; }

53 Basic Linked List - Insert Node void Linked_List::Insert_Node (ListElement In_Value) // Add Node into the start of a Linked List. { Node *New_Node = new Node; // Store the value of the node. New_Node->Value = In_Value; // The Next Node in the list is currently the First Node. New_Node->Next = First; // Make our new node the new First Node. First = New_Node; }

54 Basic Linked List - Overloaded Output << Operator ostream& operator << (ostream& out, const Linked_List& My_List) { Node *Curr_Node; Curr_Node = My_List.First; // Loop through all nodes in the list. while (Curr_Node != NULL) { out Value; // Display each Node's Value Curr_Node = Curr_Node->Next; // Point at the next node in the list } out << endl; return out; }

55 Basic Linked List - simple Main program void main() { Linked_List My_List; // Define a Linked List My_List.Insert_Node (1.1); // Add in a bunch of nodes My_List.Insert_Node (2.2); My_List.Insert_Node (3.3); cout << "\n" << My_List; // Display the list }

56 Basic Linked List - What really is happening? Declaring the Linked List :Linked_List My_List; Would simply set First to NULL, so we have a pointer pointing at the null memory address. Inserting the first node :My_List.Insert_Node (1.1); Inserting the second node :My_List.Insert_Node (2.2); 1.1 Next First NULL 2.2 Next First NULL 1.1 Next

57 Linked Lists - Find All Nodes with a particular Value void Linked_List::Find_Nodes_With_Value (ListElement In_Value) // Find ALL Nodes in a Linked List with a particular value stored in them. { // Allocate a working Node. Node *Curr_Node = First; int Node_Count = 0; // Keep track of the number of Nodes found. // Skip through the Linked List and list all nodes we find with the required value. cout << "\nNodes with value of " << In_Value << " : ";

58 Linked Lists - Find All Nodes with a particular Value while (Curr_Node != NULL) { Node_Count++; if (Curr_Node->Value == In_Value) cout << Node_Count << ", "; Curr_Node = Curr_Node->Next; } if (Node_Count == 0) cout << "No Nodes Found !!"; cout << endl; }

59 Linked Lists - Find All Nodes with a particular Value Example Usage : Find_Nodes_With_Value (3.3); will display all Nodes in the linked list with a value of 3.3. If none are found, a “None Found” message will be displayed.

60 Linked Lists Delete a Node from the List We will now look at a function to delete a particular node in the list. Remember: –When we insert a new node, it is our new FIRST node. –We have a LIFO queue - Last In First Out. –So, when we delete node number 1, we are actually deleting the first node in the list, which is the node most recently added to the list!

61 Linked Lists Delete a Node from the List void Linked_List::Delete_Node (unsigned int Node_Num) // Delete a Node from a Linked List. { // Allocate Del and Prev Nodes. Node *Del_Node = First; Node *Prev_Node = First; // Skip through the Linked List to get to the required node. for (int i = 1; i < Node_Num; i++) { if (Del_Node->Next == NULL) // Stop when there are no more nodes. break; Prev_Node = Del_Node; Del_Node = Del_Node->Next; }

62 Linked Lists Delete a Node from the List // Make sure we are deleting a valid Node. if (Node_Num > 0) { if (Node_Num == 1) // If we are deleting the first node, make First point at the second // node in the list. First = Del_Node->Next; else // Point the previous node to the node after the one to be deleted. Prev_Node->Next = Del_Node->Next; // Delete the required Node. delete Del_Node; }

63 Linked Lists Delete a Node from the List Delete_Node (0) will delete nothing. There is not a 0’th node number in the list. Delete_Node (1) will delete the first node in the list. (i.e. the last node inserted into the list, since nodes are inserted at the start of the list by the Insert function. Remember : LIFO! Delete_Node (2) will delete the second node. (i.e. the second last node inserted into the list).

64 Linked Lists Delete a Node from the List Delete_Node (50) will delete the last node if there are 50 nodes in the list. (i.e. the very first node inserted into the list). Delete_Node (99) when there are only 50 nodes in the list will delete the last node in the list. (i.e. the very first node inserted into the list).

65 Some Points on Linked Lists When working with pointer data structures (such as Linked Lists) it is important that you use diagrams when developing or interpreting code, even experienced code writers make logic errors. Be very careful when inserting or deleting nodes so that links between nodes are not broken. It is crucial that you ensure your code works with all nodes - especially the first and last nodes in the list.

66 Generic Linked List Class Let’s start of with an extremely basic class - called Animals. Then, we will include this class in a Liked List class, and use the member functions of our Animals class to do any of the input / output for Animals. In this way, by changing a single line of code in our Linked List class, we can make our Linked List work with any kind of data in any kind of class.

67 Generic Linked List - Animals Class Header class Animal { private: float Height; char Name[30]; int Age; public: Animal(); // Constructor. };

68 Generic Linked List - Animals Class Member Functions Animal::Animal()// Constructor. { Anim_Height = 0; Anim_Name [0] = '\0'; Anim_Age = 0; }

69 Generic Linked List - Basic Linked List Class Header typedef Animal List_Element; struct Node { List_Element Value;// We are now storing Animals data Node* Next; }; class Linked_List { private: Node *First; public: Linked_List(); // Constructor. void Insert_Node (); };

70 Generic Linked List - Basic Linked List Class Member Functions Linked_List::Linked_List()// Constructor. (Same as earlier in Lecture) { // Create an empty list. First = NULL; }

71 Generic Linked List - Basic Linked List Class Member Functions void Linked_List::Insert_Node () // Add Node into the start of a Linked List { // Allocate New Node. Node *New_Node = new Node; // Get and store the value of the new node. // The Next Node in the list is the current First Node. New_Node->Next = First; // Make our new node the new First Node. First = New_Node; }

72 Generic Linked List - Main Program void main() { Linked_List My_List; // Define a Linked List - of Animals My_List.Insert_Node (); // insert an item in the list cout << "\n\n" << My_List;// Display the list }

73 Classes of Classes We have a class (Animals) which has private data and various member functions. Then, we have included this in a Linked List class, with its own private data and its own member functions. When C++ notices that we are manipulating the Linked List, then the Linked Lists functions are called. If any of these Linked List functions manipulate any Animals data, then the Animals functions are called automatically by C++.

74 In exactly this way, classes can be combined and extremely complex systems can be built, and at each level there are member functions doing their job on the private data of the class they belong to. Some of the most complex human creations are built like this, for example : –Operating Systems –Games –Business Applications –Network Software Classes of Classes

75 Very Brief Introduction to Stacks and Queues Stacks and Queues are both special types of data structures or ADTs that may be implemented as linked lists or using an array. These ADTs are very useful for many different type of programming problems. A stack’s data : o is always added (pushed) onto the front of the list / array, and, o removed (popped) from the front of the list / array. This type of data structure is called a Last In First Out (LIFO) structure. That is the last item added is the first removed.

76 Stack as a linked list To get to a particular number in the stack, we would need to keep removing values off the top of the stack until we got to the required value.

77 In contrast, a queue’s data : –is always added onto the end of the list, and, –removed from the front of the list. This type of data structure is called a First In First Out (FIFO) structure. Just like a queue at a super-market checkout or a bank Queues

78 Queue as a linked list To get to a particular number in the queue, we would need to keep removing values off the queue (i.e. to the left) until we got to the required value.

79 79 End of Lecture Next time, lists continued…


Download ppt "1 Working with Pointers An exercise in destroying your computer."

Similar presentations


Ads by Google