Course notes set 8 linear lists simulated pointer implementation l.jpg
Advertisement
This presentation is the property of its rightful owner.
1 / 27

Course Notes Set 8: Linear Lists: Simulated Pointer Implementation PowerPoint PPT Presentation

Course Notes Set 8: Linear Lists: Simulated Pointer Implementation COMP 2210 Dr. Hendrix Computer Science and Software Engineering Auburn University Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.

Download Presentation

Course Notes Set 8: Linear Lists: Simulated Pointer Implementation

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Course notes set 8 linear lists simulated pointer implementation l.jpg

Course Notes Set 8:Linear Lists:Simulated PointerImplementation

COMP 2210

Dr. Hendrix

Computer Science and Software Engineering

Auburn University


Limitations of java pointers l.jpg

Limitations Of Java Pointers

May be used for internal data structures only.

Data structure backup requires serialization and deserialization.

No arithmetic.


Memory layout l.jpg

Memory Layout

c

a

e

d

b

Data structure memory is an array, and each array position has an element field (type Object) and a next field (type int).


Node representation l.jpg

Node Representation

class SimulatedNode

{

// package visible data members

Object element;

int next;

// constructors not shown

}


How it all looks l.jpg

How It All Looks

c

a

e

d

b

14

firstNode = 4

next

11

14

-1

8

0

c

a

e

d

b

element

0

1

2

3

4

5

8

11

14


Still drawn the same l.jpg

Still Drawn The Same

firstNode

-1

a

b

c

d

e


Slide7 l.jpg

Exercise 1 p 204 Sahni with Simulated Pointers

Initial state

firstNode = 4

next

  • 14

11

14

-1

8

0

c

a

e

d

b

element

0

1

2

3

4

5

8

11

14

add(0,f)

firstNode = 1

next

  • 14

4

11

14

-1

8

0

f

c

a

e

d

b

element

0

1

2

3

4

5

8

11

14


Slide8 l.jpg

Exercise 1 p 204 Sahni with Simulated Pointers

add(3,g)

firstNode = 1

next

  • 14

4

11

0

14

-1

8

2

f

g

c

a

e

d

b

element

0

1

2

3

4

5

8

11

14

add(7,h)

firstNode = 1

next

  • 14

4

11

0

14

3

8

2

-1

f

g

c

a

e

d

b

h

element

0

1

2

3

4

5

8

11

14


Slide9 l.jpg

Exercise 1 p 204 Sahni with Simulated Pointers

remove(0)

firstNode = 4

next

  • 14

11

0

14

3

8

2

-1

g

c

a

e

d

b

h

element

0

1

2

3

4

5

8

11

14

remove(4)

firstNode = 4

next

  • 14

0

14

3

2

8

-1

g

c

a

e

b

h

element

0

1

2

3

4

5

8

11

14


Memory management l.jpg

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 l.jpg

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 in use.

Compact free space (optional).

Move free nodes to storage pool.


Marking l.jpg

Marking

c

a

e

d

b

firstNode

Unmark all nodes.

Start at each program variable that contains a reference, follow all pointers, mark nodes that are reached.


Marking13 l.jpg

Marking

c

c

a

a

e

e

d

d

b

e

firstNode

Start at firstNode and mark all nodes reachable from firstNode.

Repeat for all reference variables.


Compaction l.jpg

Compaction

Free Memory

c

a

e

d

b

e

d

b

firstNode

Move all marked nodes (I.e., nodes in use) to one end of memory, updating all pointers as necessary.


Put free memory in storage pool l.jpg

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 l.jpg

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 collection17 l.jpg

Advantages Of Garbage Collection

Applications may run faster when run on computers that have more memory.


Disadvantage of garbage collection l.jpg

Disadvantage Of Garbage Collection

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


Alternative to garbage collection l.jpg

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 l.jpg

Advantage Of Alternative

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


Disadvantages of alternative l.jpg

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 l.jpg

Storage Pool Organization When All Nodes Have Same Size

firstNode

null

a

b

c

d

e

  • Maintain a chain of free nodes

  • Allocate from front of chain

  • Add node that is freed to chain front


Simulated pointer memory management l.jpg

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

}


Constructor l.jpg

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 l.jpg

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;

}


Free a node l.jpg

Free A Node

public void deallocateNode(int i)

{// Free node i.

// make i first node on free space list

node[i].next = firstNode;

firstNode = i;

// remove element reference so that space can be garbage

// collected

node[i].element = null;

}


Simulated pointers l.jpg

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.

Don’t use unless you see a clear advantage to using simulated pointers over Java references.


  • Login