CS 1114: Data Structures – Implementation: part 1 Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

CHP-5 LinkedList.
Part IV: Memory Management
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 6 Data Types
Lecture 10: Heap Management CS 540 GMU Spring 2009.
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4.
Memory Management Chapter 4. Memory hierarchy Programmers want a lot of fast, non- volatile memory But, here is what we have:
Memory Management Memory Areas and their use Memory Manager Tasks:
Linked lists Prof. Noah Snavely CS1114
Introduction and a Review of Basic Concepts
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
CS Data Structures Chapter 4 Lists.
1 Dynamic Memory Management. 2 What’s worse?  50% of instructions/data are cache misses (fetched from RAM)  1% of instructions/data are page faults.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CY2003 Computer Systems Lecture 09 Memory Management.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 4 Memory Management.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Storage allocation issues Prof. Ramin Zabih
1 Writing a Good Program 8. Elementary Data Structure.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Memory Management COSC 513 Presentation Jun Tian 08/17/2000.
Memory Management. Memory  Commemoration or Remembrance.
Memory Management Problem: Records (of various lengths) need to be stored. Model: A big array of space to store them, managed by a memory manager. Like.
Copyright © Genetic Computer School 2008 Computer Systems Architecture SA 7- 0 Lesson 7 Memory Management.
Memory Management -Memory allocation -Garbage collection.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Memory Management Damian Gordon.
CSE 220 – C Programming malloc, calloc, realloc.
Storage Allocation Mechanisms
Memory Management Memory Areas and their use Memory Manager Tasks:
Storage 18-May-18.
Chapter 2 Memory and process management
Prof. Ramin Zabih Implementing queues Prof. Ramin Zabih
CS 1114: Implementing Search
Dynamic Memory Allocation
Storage Management.
Chapter 4 Linked Lists.
Memory Management Memory Areas and their use Memory Manager Tasks:
Garbage collection; lightstick orientation
Main Memory Management
Pointers and Dynamic Variables
Simulated Pointers.
Simulated Pointers.
Dynamic Memory.
Presentation made by Steponas Šipaila
Linked lists Prof. Noah Snavely CS1114
Memory Management Memory Areas and their use Memory Manager Tasks:
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Dynamic Memory Management
Run-time environments
Presentation transcript:

CS 1114: Data Structures – Implementation: part 1 Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)

Linked lists – running time  We can insert an item (at the front) in constant (O(1)) time –Just manipulating the pointers –As long as we know where to allocate the cell –If we need to insert an item inside the list, then we must first find the place to put it.  We can delete an element (at the front) in constant time –If the element isn’t at the front, then we have to find it … how long does that take? 2

Linked lists – running time  Finding the place to insert/delete …  Easier case: –What about inserting / deleting from the end of the list?  How can we fix this? 3

4 Doubly linked lists Singly linked representation Doubly linked representation

5 A doubly-linked list in memory First element Last element Size of list XX

Doubly-linked list insertion 6 Initial list Insert a 5 at end Insert an 8 at the start XXX 456 XXX 789 XXXX XXX 789 XXXX XXXX

Memory allocation  When we need a new cell, how do we know where to find it?  We’ll keep track of a “free pointer” to the next unused cell after the list First element Last element Size of list XX Next free cell

Doubly-linked list insertion 8 Insert a 9 at the start XXX 789 XXXX XXXX Delete the last element XXXX

9 Memory allocation  Current strategy: when we need more storage, we just grab locations at the end  What can go wrong?  When we delete items from a linked list we change pointers so that the items are inaccessible –But they still waste space!

Memory allocation  Strategy 1: Computer keep tracks of free space at the end  Strategy 2: Computer keeps a linked list of free storage blocks (“freelist”) –For each block, stores the size and location –When we ask for more space, the computer finds a big enough block in the freelist –What if it doesn’t find one? 10

Maintaining a freelist XXXX Delete the last element XXX XXXX Start: 10 Free space: 999,990 Start: 10 Free space: 999,990 Start: 4 Free space: 3 Free list

12 Allocation issues  Surprisingly important question: –Which block do you supply? –The smallest one that the users request fits into? –A larger one, in case the user wants to grow the array?

Memory deallocation  How do we give the computer back a block we’re finished with?  Someone has to figure out that certain values will never be used ever (“garbage”), and should be put back on the free list –If this is too conservative, your program will use more and more memory (“memory leak”) –If it’s too aggressive, your program will crash (“blue screen of death”) 13

Memory deallocation  Two basic options: 1.Manual storage reclamation –Programmer has to explicitly free garbage –Languages: C, C++, assembler 2. Automatic storage reclamation –Computer will notice that you’re no longer using cells, and recycle them for you –Languages: Matlab, Java, C#, Scheme 14