Docsity

Simulated Pointers - Lecture Slides | COP 3530, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Simulated Pointers - Lecture Slides | COP 3530 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Simulated Pointers

Limitations Of Java Pointers^ • May be used for internal data

structures only.

  • Data structure backup requires serialization and deserialization. - No arithmetic.

Node Representation

package dataStructures; class SimulatedNode {

// package visible data members Object element; int

next; // constructors not shown }

How It All Looks

c a e d b 14 c a e d b 14 0 11 8

next element 0 1 2 3 4 5 8 11 14 firstNode = 4

Memory Management

Linked system (Java 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 Java has the method new - way to free a node that is no longer in use Java uses garbage collection

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

c a e d b firstNode c a e d e Start at firstNode and mark all nodesreachable from firstNode. Repeat for all reference variables.

Compaction

Move all marked nodes (i.e., nodes in use) to one end of memory,updating all pointers as necessary. c b e d b firstNode a e d Free Memory

Advantages Of Garbage Collection

  • Programmer doesn’t have to worry about freeing nodes as they becomefree. - However, for garbage collection to be effective, we must set referencevariables to null when the objectbeing referenced is no longerneeded.

Advantages Of Garbage Collection

  • Applications may run faster when run on computers that have morememory.

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 andnot to total memory size.

Storage Pool Organization When All

Nodes Have Same Size

a b c d e null firstNode

  • Maintain a chain of free nodes• Allocate from front of chain• Add node that is freed to chain front

Simulated-Pointer Memory Management

/** memory management for simulated pointer classes / package dataStructures; import utilities.; public class SimulatedSpace1 { // data members private int firstNode; SimulatedNode [] node; // package visible // constructor and other methods come here }