Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.

Slides:



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

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
CHP-5 LinkedList.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
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.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CS 1114: Data Structures – Implementation: part 1 Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Pointers Applications
L.Chen1 Chapter 3 DATA REPRESENTATION(2) L.Chen A Chain Iterator Class A Chain Iterator Class ( 遍历器类 )  An iterator permits.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
SNU IDB Lab. Ch7. Linear Lists – Simulated Pointers © copyright 2006 SNU IDB Lab.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6C Pointers,
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Pointers OVERVIEW.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Object-Oriented Programming in C++
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Memory Management -Memory allocation -Garbage collection.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
Garbage Collection What is garbage and how can we deal with it?
Storage 18-May-18.
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Unit-2 Objects and Classes
UNIT-3 LINKED LIST.
Data Structures Interview / VIVA Questions and Answers
Concepts of programming languages
Simulated Pointers.
Simulated Pointers.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Data Structures & Algorithms
List Allocation and Garbage Collection
LINKED LIST Dr. T. Kokilavani Assistant Professor
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Simulated Pointers

Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization. No arithmetic.

Simulated-Pointer Memory Layout Data structure memory is an array, and each array position has an element field (type T) and a next field (type int). caedb

Node Representation template class simulatedNode { public: // constructors defined here protected: T element; int next; }

How It All Looks 14 caedb caedb firstNode = 4 next element

Still Drawn The Same abcde firstNode

Memory Management Linked system (C++ or simulated pointer) requires: a way to keep track of the memory that is not in use (i.e., a storage pool) way to allocate a node C++ has the method new way to free a node that is no longer in use C++ has the method delete

Garbage Collection The system determines which nodes/memory are not in use and returns these nodes (this memory) to the pool of free storage. This is done in two or three steps: Mark nodes that are in use. Compact free space (optional). Move free nodes to storage pool.

Marking Unmark all nodes (set all mark bits to false). Start at each program variable that contains a reference, follow all pointers, mark nodes that are reached. caedb firstNode

Marking caedb firstNode caede Repeat for all pointer variables. Start at firstNode and mark all nodes reachable from firstNode.

Compaction Move all marked nodes (i.e., nodes in use) to one end of memory, updating all pointers as necessary. cbedb firstNode aed Free Memory

Put Free Memory In Storage Pool Scan memory for unmarked nodes (if no compaction done), otherwise put single free block (unless no free memory) into pool.

Advantages Of Garbage Collection Programmer doesn’t have to worry about freeing nodes as they become free. However, for garbage collection to be effective, we must set reference variables to null when the object being referenced is no longer needed.

Advantages Of Garbage Collection Applications may run faster when run on computers that have more memory.

Disadvantage Of Garbage Collection Garbage collection time is linear in memory size (not in amount of free memory).

Alternative To Garbage Collection Provide a method to free/deallocate a node. e.g., delete method of C++ Now free nodes are always in storage pool.

Advantage Of Alternative Time to free nodes is proportional to number of nodes being freed and not to total memory size.

Disadvantages Of Alternative User must write methods to free data structure nodes. Time is spent freeing nodes that may not be reused. Application run time does not improve with increase in memory size.

Storage Pool Organization When All Nodes Have Same Size abcde firstNode Maintain a chain of free nodes Allocate from front of chain Add node that is freed to chain front

Simulated-Pointer Memory Management template class simulatedSpace { public: // constructor and other methods // defined here protected: int firstNode; simulatedNode *node; }

Constructor template simulatedSpace (int numberOfNodes) { node = new simulatedNode [numberOfNodes]; // create nodes and link into a chain for (int i = 0; i < numberOfNodes - 1; i++) node[i].next = i + 1; // last node of array and chain node[numberOfNodes - 1].next = -1; // first node of chain of free nodes firstNode = 0; }

Allocate A Node template int allocateNode(T& element, int next) {// Allocate a free node and set its fields. if (firstNode == -1) {// double number of nodes, code omitted } int i = firstNode; // allocate first node firstNode = node[i].next; node[i].element = element; node[i].next = next; return i; }

Free A Node template void deallocateNode(int i) {// Free node i. // make i first node on free space list node[i].next = firstNode; firstNode = i; }

Simulated Pointers Can allocate a chain of nodes without having to relink. Can free a chain of nodes in O(1) time when first and last nodes of chain are known.

Simulated Pointers Don’t use unless you see a clear advantage to using simulated pointers over C++ pointers.