linked lists& dynamic memory errors · detecting memory errors •valgrindis a tool that...

14
LINKED LISTS & DYNAMIC MEMORY ERRORS Problem Solving with Computers-I

Upload: others

Post on 22-Sep-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Next time• Recursion