Docsity

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

Partial preview of the text

Download Simulated Pointers in Data Structures and Algorithms - 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.

Simulated-Pointer Memory Layout

Data structure memory is an array,

and each array position has an

element field (type Object) and a

next field (type int).

c a e d b

Node Representation

package dataStructures;

class SimulatedNode

// package visible data members

Object element;

int next;

// constructors not shown

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

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.

c a e d b

firstNode

Marking

c a e d b

firstNode

c a e d e

Repeat for all reference variables.

Start at firstNode and mark all nodes

reachable from firstNode.

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.

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 SimulatedSpace { // data members private int firstNode; SimulatedNode [] node; // package visible

// constructor and other methods come here }

Constructor

public SimulatedSpace1(int numberOfNodes) { node = new SimulatedNode [numberOfNodes];

// create nodes and link into a chain for (int i = 0; i < numberOfNodes - 1; i++) node[i] = new SimulatedNode(i + 1);

// last node of array and chain node[numberOfNodes - 1] = new SimulatedNode(-1); // firstNode has the default initial value 0 }

Allocate A Node

public int allocateNode(Object 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; // firstNode points to next free node node[i].element = element; node[i].next = next; return i; }

Simulated Pointers

• Don’t use unless you see a clear advantage to

using simulated pointers over Java references.