Circular Buffers, Linked Lists

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Memory Allocation CS Introduction to Operating Systems.
Pointers OVERVIEW.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
LINKED LISTS.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Lecture 6 of Computer Science II
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Pointers and Linked Lists
Linked List :: Basic Concepts
Pointers and Linked Lists
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Data Structure and Algorithms
C Programming Language Review and Dissection IV
Pointers and Memory Overview
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Lists.
CS 240 – Lecture 16 Structs, Struct Arrays, Struct Pointers, sizeof, malloc, free, void pointer.
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Lists.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
CS Introduction to Operating Systems
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Circular Buffers.
Pointers and Linked Lists
Dynamic Memory Allocation
Memory Allocation CS 217.
Lecture 18 Arrays and Pointer Arithmetic
Linked Lists.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Dynamic Memory A whole heap of fun….
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Linked List.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists.
Dynamic Memory A whole heap of fun….
Lists.
Data Structures and Algorithms Introduction to Pointers
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Chapter 10-1: Dynamic Memory Allocation
Week 7 - Friday CS222.
SPL – PS2 C++ Memory Handling.
Module 13 Dynamic Memory.
Presentation transcript:

Circular Buffers, Linked Lists CS 240 – Lecture 17 Circular Buffers, Linked Lists

Data Structure – Circular Buffer A circular buffer is a data structure consisting of three parts: A fixed-length buffer/array. A head index. A tail index. The general purpose of a Circular Buffer is to receive a stream of data (usually arbitrarily long or infinite) but to only house a finite amount of it in memory at any time.

Circular Buffer The primary mechanism for the Circular Buffer is to treat a normal buffer as if the next element after the last space in the buffer is going to be the first element of the buffer. Take the circular buffer above and count up from 3 to 9.

Circular Buffer As you can see, the data rolls around the end of the buffer, back to the beginning. However, if we want to retrieve the data in the order in which we received it, we need to keep track of where to place the freshest element and the locate on the stalest element. When the buffer is full, as seen above, it depends on the application if we should replace the stalest element or wait until the stalest element is removed.

Circular Buffer Here we include two indexes for the circular buffer: Head and Tail. Head indicates the beginning of stored data, where we have the stalest element. Tail indicates the location immediately after stored data, where the freshest element will go. In this case, the nine from our last example is the freshest element.

Circular Buffer When the buffer is full, both Tail and Head point to the same element in the underlying buffer. This means that if we want to accept new data (let's say 10) into the buffer, we need to replace the oldest data (3) with the newest data (10). Again, replacing old data with new data may need to wait until the old data is used, depending on the application.

Circular Buffer When removing the oldest element, the head index moves over to the next element. When adding an element, the tail index moves over to the next element.

Circular Buffer When the buffer is full and you still add an element, the stalest element is over written and both head and tail are moved over to the next element. Stalest element (3) is lost in this case.

Circular Buffer Accessing the elements of the circular buffer from the underlying buffer requires some mathematical tricks. In this case, the head element (the first, the stalest, the oldest) is at index 5 in the underlying buffer. The tail space in the buffer is at 4, with the last, stalest element at 3.

Circular Buffer – Indexing We need to find a way to associate the following circular buffer indexes with the underlying actual indexes. The way we do this is with modular arithmetic: i = (head + j) % length; j = (i – head + length) % length; i j

Circular Buffer – Indexing When shifting the head and tail indices, you also need modular arithmetic to avoid those indices from falling off the buffer. head = (head + 1) % length; tail = (tail + 1) % length;

Circular Buffer – Example Code for Circular Buffer of Characters int head = tail = 0; int count = 0; char array[10]; char get(int j) { int i = (head + j) % 10; return array[i]; } char poll() { char ret = array[head]; head = (head + 1) % 10; count--; return ret; void push(char x) { array[tail] = x; if (head == tail && count == 10) { head = (head + 1) % 10; tail = (tail + 1) % 10; } else { count++; } These get and poll functions do NOT check for content or count and are only for demonstration.

Data Structure – Limitations of Fixed Storage The dependence on underlying fixed storage for many data structures is unrealistic. The data structure has a size limit that cannot be overcome once it is decided. There must be enough contiguous space for the entire structure. We'll tackle the first problem using what we learned about dynamic allocation. Then, we'll discuss a new type of data structure that solves the second problem.

Memory – realloc Once you've requested a block of memory with malloc, you fall into the same pitfall as allocating memory with an array: the fixed size. However, the malloc suite from stdlib.h includes a function called realloc. void *realloc( void *ptr, size_t new_size ); When realloc is called, it attempts to find a new block of memory of the new size. If it finds one, realloc copies the content of the block at ptr to the new block, frees the old block, and returns a pointer to that new block. Otherwise, it returns the null pointer and the original arrangement is kept.

Circular Buffer – Revisited int head = tail = 0; int count = 0, size = 10; char* array; void initialize(){ array = (char*) malloc(size); } char get(int j) { int i = (head + j) % size; return array[i]; char poll() { char ret = array[head]; head = (head + 1) % size; count--; return ret; void push(char x) { array[tail] = x; if (head == tail && count == size) { head = (head + 1) % size; tail = (tail + 1) % size; } else { count++; } int resize(int newsize) { char* newblock = (char*) realloc(newsize); if (newblock) { array = newblock; size = newsize; return newblock != NULL;

Data Structure – Linked List Linked list data structures solve both the limited size and contiguity problems that arrays suffer. struct link { struct link *next; type element; } At it's core, a Linked List is composed of smaller structures called links or nodes. The list has one link called the "root" or "head" element, and that's considered index 0. Other links are given an index based on how many connections away they are from being the root link.

Linked List – The Empty List To store a linked list as a variable, we do not use the link struct directly. struct link *root; Instead, we keep a pointer to the root link. Why? An empty list has no links, but it's still a Linked List. struct link *root = NULL; The above is a valid Linked List. Specifically NULL is the empty Linked List. root

Linked List – Adding Links To add links to a Linked List, we need to find the end of the list. Whatever we add to the list will be added after the last link. struct link *current = root; while (current && current->next) current = current->next; The above code finds the last link in the list; current points to it. There are two cases for the value of current. current is NULL root is the Empty Linked List current is the address of a link there is at least one link in the list

Linked List – Adding Links In either case, we need to create a new link. struct link *newlink = (struct link*) malloc(sizeof(struct link)); This allocates persistent memory for a new link somewhere in memory. We just requested this memory from malloc, but it contains junk. newlink->next = NULL; newlink->element = <SOMETHING>; Now we need to decide what to do with this new link based on the cases.

Linked List – Adding Links In the case of the empty list, we have root == NULL That means that we need the new link we're adding to be the new head of the list. root = newlink; In the general case, current is a node that currently links to NULL We want it to link to newlink instead. current->next = newlink; root newlink root current current

Linked List – Deleting Links Let's say that we have the address of a target link in a list that we want to remove. struct link *target = <SOMETHING>; First, we need to find the link's previous link: struct link *curr = root; while (curr && curr->next && curr != target) curr = curr->next; Either curr is target's previous link or target is not in root's list.

Linked List – Deleting Links Let's assume that target is in the list. We want to remove the target link and close the gap between other elements of the list. After searching for target's previous, we get curr->next = target->next; The list now has no reference to target. Are we done? root target root

Linked List – Deleting Links We're not done. Remember, each link is created by allocating memory with malloc. If we're not using that memory anymore, we need to free it back to the allocation buffer. free(target); If we do not release that memory, then the application using the linked list will cause a memory leak when it deletes links.

Linked List – Closing Remarks As a result of the Linked List's composition, we end up with the following conclusions to make about it. The linked list can have as many links as there are available addresses in memory. The linked list can add elements as long as there is at least a block of 4 bytes + the size of the element available to malloc. Compared to an array implementation. Arrays are limited to a built-in SIZE_MAX, which is at least 65535 but on modern 32-bit machines can be limited to 232-1. Some implementations allow up to 264-1 on 64-bit architectures which is essentially limitless as well. Arrays must be allocated all at once contiguously. If there is no sufficiently-sized contiguous space, the array cannot exist.