more on dynamic memory allocation seokhee jeon department of computer engineering kyung hee...
TRANSCRIPT
![Page 1: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/1.jpg)
More on Dynamic Memory AllocationMore on Dynamic Memory Allocation
Seokhee Jeon
Department of Computer Engineering
Kyung Hee University
•1
Illustrations, examples, and text in the lecture note courtesy of Prof. David Bernstein, https://users.cs.jmu.edu/bernstdh/web/common/lectures/slides_cpp_dynamic-memory.php
![Page 2: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/2.jpg)
RemindersReminders
• Dynamically allocated memory is kept on the memory heap
• Dynamically allocated memory can't have a "name" it must be referred by pointers
• Declarations are used to statically allocate memory, the new operator is used to dynamically allocate memory
![Page 3: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/3.jpg)
Pointing to Memory Allocated at Run Pointing to Memory Allocated at Run TimeTime
![Page 7: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/7.jpg)
Returning Memory to the HeapReturning Memory to the Heap
• How Big is the Heap?
– It can only contain as much physical memory as you have installed or as much virtual memory as your operating system can make available (if it supports virtual memory)
• Running Out of Memory:
– Most applications request memory from the heap when they are running
– It is possible to run out of memory (you may even have gotten a message like "Running Low On Virtual Memory")
– So, it is important to return memory to the heap when you no longer need it
![Page 9: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/9.jpg)
Returning Memory to the HeapReturning Memory to the Heap
• Dangling Pointers:
– The delete operator does not delete the pointer, it takes the memory being pointed to and returns it to the heap
– It does not even change the contents of the pointer
– Since the memory being pointed to is no longer available (and may even be given to another application), such a pointer is said to be dangling
![Page 10: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/10.jpg)
Returning Memory to the HeapReturning Memory to the Heap
• Remember:– Return memory to the heap before undangling the
pointer• What's Wrong with the Following:
– ptr = NULL;– delete ptr;
![Page 11: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/11.jpg)
Returning Memory to the HeapReturning Memory to the Heap
• What About Arrays?– You want to return all of the memory to the heap– So, a different form of the delete operator is
needed– Also, the memory allocator must keep track of the
size of the array
![Page 13: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/13.jpg)
Memory LeakMemory Leak
• Memory leaks when it is allocated from the heap using the new operator but not returned to the heap using the delete operator
![Page 17: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/17.jpg)
Linked Lists in Linked Lists in CC and and C++C++
CS-2303System Programming Concepts
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
•17
![Page 18: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/18.jpg)
DefinitionsDefinitions
• Linked List• A data structure in which each element is
dynamically allocated and in which elements point to each other to define a linear relationship
• Singly- or doubly-linked• Stack, queue, circular list
18
Note: elements are usually the
same type (but not always).
![Page 19: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/19.jpg)
Linked ListLinked List
struct listItem {type payload;struct listItem *next;
};
19
payload
next
payload
next
payload
next
payload
next
Note: payload may be
multiple members.
![Page 20: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/20.jpg)
Linked List Linked List (continued)(continued)
• Items of list are usually same type• Generally obtained from new operator
• Each item points to next item• Last item points to null• Need “head” to point to first item!
• “Payload” of item may be almost anything• A single member or multiple members• Any type of object whose size is known at compile time• Including struct, union, char * or other pointers• Also arrays of fixed size at compile time (see p. 214)
20
![Page 21: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/21.jpg)
Usage of Linked ListsUsage of Linked Lists
• Not massive amounts of data• Linear search is okay
• Sorting not necessary• or sometimes not possible
• Need to add and delete data “on the fly”• Even from middle of list
• Items often need to be added to or deleted from the “ends”
21
![Page 22: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/22.jpg)
Linked List Linked List (continued)(continued)
struct listItem {type payload;struct listItem *next;
};struct listItem *head;
22
payload
nextpayload
nextpayload
next
payload
next
![Page 23: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/23.jpg)
Adding an Item to a ListAdding an Item to a List
struct listItem *p, *q;• Add an item pointed to by q after item pointed to by p
– Neither p nor q is NULL
23
payload
nextpayload
nextpayload
next
payload
next
payload
next
![Page 24: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/24.jpg)
Adding an Item to a ListAdding an Item to a List
listItem *addAfter(listItem *p, listItem *q){q -> next = p -> next;p -> next = q;return p;
}
24
payload
nextpayload
nextpayload
next
payload
next
payload
next
![Page 25: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/25.jpg)
Adding an Item to a ListAdding an Item to a List
listItem *addAfter(listItem *p, listItem *q){q -> next = p -> next;p -> next = q;return p;
}
25
payload
nextpayload
nextpayload
next
payload
next
payload
next
![Page 26: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/26.jpg)
Adding an Item to a ListAdding an Item to a List
listItem *addAfter(listItem *p, listItem *q){q -> next = p -> next;p -> next = q;return p;
}
26
payload
nextpayload
nextpayload
next
payload
next
payload
next
Question: What to do if we cannotguarantee that p and q are non-NULL?
![Page 27: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/27.jpg)
Adding an Item to a List Adding an Item to a List (continued)(continued)
listItem *addAfter(listItem *p, listItem *q){if (p && q) {
q -> next = p -> next;p -> next = q;
}return p;
}
27
payload
nextpayload
nextpayload
next
payload
next
payload
next
Note test for non-null p and q
![Page 28: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/28.jpg)
What about Adding an ItemWhat about Adding an Itembeforebefore another Item? another Item?
struct listItem *p;• Add an item before item pointed to by p (p != NULL)
28
payload
nextpayload
nextpayload
next
payload
next
payload
next
![Page 29: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/29.jpg)
What about Adding an ItemWhat about Adding an Itembeforebefore another Item? another Item?
• Answer:–– Need to search list from beginning to find previous
item– Add new item after previous item
• This is needed in PA#3– Insert item after earlier event times and before
later ones– Need to search the list
29
![Page 30: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/30.jpg)
Doubly-Linked ListDoubly-Linked List
struct listItem {type payload;listItem *prev;listItem *next;
};struct listItem *head, *tail;
30
prev next
payload
prev next
payloadprev next
payload
prev next
payload
In-class exercise
:– how to
add a new item q after a lis
t
item p
![Page 31: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/31.jpg)
Other Kinds of List StructuresOther Kinds of List Structures
• Queue — FIFO (First In, First Out)
• Items added at end• Items removed from beginning
• Stack — LIFO (Last In, First Out)
• Items added at beginning, removed from beginning
• Circular list• Last item points to first item• Head may point to first or last item• Items added to end, removed from beginning 31
![Page 32: More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649d055503460f949d8eb8/html5/thumbnails/32.jpg)
Circular ListCircular List
listItem *addAfter (listItem *p, listItem *tail){if (p && tail) {
p -> next = tail -> next;tail = p;
} else if (p) {tail p -> next = p;
}return tail;
}32
payload
nextpayload
nextpayload
next
payload
next
struct listItem *tail;
Optional:–struct listItem *head;