Linked Memory Pointers Linked Lists January 19, 2018 Cinda Heeren / Geoffrey Tien 1
Addresses and pointers Every storage location in memory (RAM) has an address associated with it The address is the location in memory where a given variable or identifier stores its data Can think of address in memory like a mailbox number Use the address to find where the mailbox is Look inside the mailbox to access the contents/value A pointer is a special type of variable That stores an address rather than a value The address is used to find a value elsewhere in memory January 19, 2018 Cinda Heeren / Geoffrey Tien 2
Declaring pointers Pointer variables are declared as follows: datatype* identifier e.g. int* ptr; or int * ptr; or int *ptr; Note that the type of a pointer is not the same as the type it points to e.g. ptr is a pointer to an int, but is itself not an int Warning! The declaration int* var1, var2; declares var1 as a pointer, but var2 as an integer! To declare both as pointers, either declare individually, or: int *var1, *var2; January 19, 2018 Cinda Heeren / Geoffrey Tien 3
Address operator and dereferencing Pointers can be assigned the address of an existing variable Using the address operator, & The value which a pointer points to can be accessed by dereferencing the pointer Using the * operator p x 4096 23 47 38 0 1 2 12 2 20-1 int x = 23; int* p = &x; x = 47; *p = 38; January 19, 2018 Cinda Heeren / Geoffrey Tien 4
Pointers as parameters Passing pointer parameters allows the function to access and/or modify the actual parameter int getarraysum(int arr[], int size, int* pcount) { int sum = 0; for (int i = 0; i < size; i++) { if (arr[i] > 0) (*pcount)++; sum += arr[i]; } return sum; } int numpositive = 0; int numbers[] = {3, 7, -9, 5, -4}; int result = getarraysum(numbers, 5, &numpositive); cout << "Array sum: " << result << endl; cout << "Number of positive elements: " << numpositive << endl; January 19, 2018 Cinda Heeren / Geoffrey Tien 5
Pointers as parameters Another example void f1(int arg) { arg = 22; printf("f1 arg: %d\n", arg); } void f2(int* arg) { *arg = 410; printf("f2 arg: %d\n", arg); } int x = 45; f1(x); printf("x after f1: %d\n", x); f2(&x); printf("x after f2: %d\n", x); January 19, 2018 Cinda Heeren / Geoffrey Tien 6
Pointer to a pointer...to a pointer to a pointer... int main() { int x = 5; int* p = &x; *p = 6; int** q = &p; int*** r = &q; } cout << "*p: " << *p << endl; cout << "*q: " << *q << endl; cout << "**q: " << *(*q) << endl; "You can keep adding levels of pointers until your brain explodes or the compiler melts whichever happens soonest" stackoverflow user JeremyP January 19, 2018 Cinda Heeren / Geoffrey Tien 7
Pointers and dynamic memory The new keyword allocates space in dynamic memory and returns the first address of the allocated space delete releases the memory at the address referenced by its pointer variable delete[] is used to release memory allocated to array variables int a = 5; int* b = new int; int* c = &a; *c = 4; int** d = &b; int* e = new int[a]; **d = 3; int* f = new int[*b]; delete b; delete e; // causes a memory leak delete[] f; January 19, 2018 Cinda Heeren / Geoffrey Tien 8
Dynamic allocation of a 2D array int dim_row = 3; int dim_col = 5; int** myarray; // pointer to a pointer stack heap myarray January 19, 2018 Cinda Heeren / Geoffrey Tien 9
Linked Lists January 19, 2018 Cinda Heeren / Geoffrey Tien 10
Linked lists Imagine an array partially filled with data And we want to insert an item in a particular position in the middle 17 A motivation 12 15 16 19 22 28 34 37 41 46 All of these elements must be shifted over one at a time Linked lists are a dynamic data structure that can achieve fast insertions/ deletions in the middle January 19, 2018 Cinda Heeren / Geoffrey Tien 11
Linked list nodes A linked list is a dynamic data structure that consists of nodes linked together A node is a data structure that contains data the location of the next node January 19, 2018 Cinda Heeren / Geoffrey Tien 12
Node pointers A node contains the address of the next node in the list In C++ this is recorded as a pointer to a node Nodes are created in dynamic memory And their memory locations are not in sequence The data attribute of a node varies depending on what the node is intended to store January 19, 2018 Cinda Heeren / Geoffrey Tien 13
Linked lists A linked list is a chain of nodes where each node stores the address of the next node Start 7 2 6 8 This symbol indicates a null pointer January 19, 2018 Cinda Heeren / Geoffrey Tien 14
Linked list implementation Node class class Node { public: int data; Node* next; }; next points to another node, hence its type Node* attributes / members of a particular node can be accessed using the '.' (dot) operator or the '->' (arrow) operator as a shorthand for pointer types equivalent to dereferencing and using dot operator Node nd; nd.data = 5; Node* p = nd.next; (*p).data = 5; Node* q = *((*p).next).next; Node* r = q->next->next; January 19, 2018 Cinda Heeren / Geoffrey Tien 15
Building a linked list Node* a = new Node(7, null); Assume we have written a parameterized constructor for the Node class a 7 January 19, 2018 Cinda Heeren / Geoffrey Tien 16
Building a linked list Node* a = new Node(7, null); a->next = new Node(3, null); a 7 3 January 19, 2018 Cinda Heeren / Geoffrey Tien 17
Traversing a linked list Node* a = new Node(7, null); a->next = new Node(3, null); Node* p = a; p = p->next; // go to next node p = p->next; a 7 3 p January 19, 2018 Cinda Heeren / Geoffrey Tien 18
Linked list insertion Insertion in a singly-linked list requires only updating the next node reference of the preceding position a 15 16 19 22 28 p 17 b Node* b = new Node(17, p->next); p->next = b; Important! Be aware that sequential nodes are not guaranteed to be found in sequential memory locations! January 19, 2018 Cinda Heeren / Geoffrey Tien 19
Linked list removal Likewise, we can remove a node by updating the pointer of the preceding node but remember to delete the removed node! a 15 16 19 22 28 p b p->next = b->next; delete b; January 19, 2018 Cinda Heeren / Geoffrey Tien 20
Readings for this lesson Carrano & Henry C2.3, C2.5 (Pointers, dynamic arrays) Chapter 4 (Linked lists) Next class: Carrano & Henry, Chapter 6 (Stacks) Background: Carrano & Henry, Chapter 1.4, C1.1-C1.4 January 19, 2018 Cinda Heeren / Geoffrey Tien 21