![Page 1: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/1.jpg)
LINKED LISTS & DYNAMIC MEMORY ERRORS
Problem Solving with Computers-I
![Page 2: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/2.jpg)
Creating a small list2
• Define an empty list
• Add a node to the list with data = 10
struct Node {int data;Node* next;
};
struct LinkedList {Node* head;Node* tail;
};e
![Page 3: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/3.jpg)
Insert into a non empty list
i oDwe
![Page 4: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/4.jpg)
Inserting a node at the head of a linked listvoid insert(LinkedList& h, int value) ;
![Page 5: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/5.jpg)
Iterating through the list/* Find the number of elements in the list */
int count(LinkedList& list);
head tail
list
![Page 6: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/6.jpg)
Deleting the list/* Free all the memory that was created on the heap*/
void freeList(LinkedList& list);
head tail
list
![Page 7: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/7.jpg)
Deleting a node from the linked list
x4 fpref IFLpreve next Curr next
delete curry11 Need a special ease to delete the Iste
![Page 8: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/8.jpg)
Double Linked Lists7
Array List1 2 3
BozFIFE
![Page 9: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/9.jpg)
Implementing a double-linked list8
• Define a node in a double linked list• Write functions to
• insert a node to the head/tail of the linked list• Print all the elements of the list• Delete a node with a given value• Free the list
![Page 10: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/10.jpg)
Dangling pointers and memory leaks
–Dangling pointer: Pointer points to a memory location that no longer exists–Memory leaks (tardy free):
• Heap memory not deallocated before the end of program
• Heap memory that can no longer be accessed
int t p k p is a dangling pointerin 9 0 Hq is not dangling
11Problem with dangling pointersis that dereferencing their
car
11 cause a kgfaultcontCC p 11Possible Segfault
If 1nshad thepointer was initializedto 0 we could do a
null check before rdeereferencing
![Page 11: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/11.jpg)
Dynamic memory pitfalls
Example
void foo(){int* p = new int;
}
Memory leaks (tardy free):Heap memory not deallocated before the end of program Heap memory that can no longer be accessed
Null check
I nonly dereferences
q if its
not null
![Page 12: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/12.jpg)
Memory errors can cause your program to crash• Segmentation faults: Program crashes because it attempted to
access a memory location that either doesn’t exist or doesn’t have permission to access
• Examples of code that results in undefined behavior and potential segmentation fault
int arr[] = {50, 60, 70};
for(int i=0; i<=3; i++){cout<<arr[i]<<endl;
}
int x = 10;int* p;cout<<*p<<endl;
![Page 13: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/13.jpg)
Detecting memory errors• Valgrind is a tool that reports errors related to dynamic memory
allocation, access and deletion
• Run valgrind on your program using the following command:
valgrind --leak-check=full ./myprog
![Page 14: LINKED LISTS& DYNAMIC MEMORY ERRORS · Detecting memory errors •Valgrindis a tool that reports errors related to dynamic memory allocation, access and deletion •Run valgrindon](https://reader033.vdocument.in/reader033/viewer/2022051911/6001c211a68642057e022f2b/html5/thumbnails/14.jpg)
Next time• Recursion