Download presentation
Presentation is loading. Please wait.
1
Simulated Pointers
2
Limitations Of C++ Pointers
May be used for internal data structures only. Data structure backup requires serialization and deserialization. No arithmetic. Can’t use C++ pointers to represent a data structure stored on a disk. Serialization and deserialization may be expensive and complex operations. In some applications it is desirable to perform arithmetic on pointers.
3
Simulated-Pointer Memory Layout
c a e d b Data structure memory is an array, and each array position has an element field (type T) and a next field (type int). In a simulated pointer representation an array of nodes is used. The linked data structure is built inside this array.
4
Node Representation template <class T> class simulatedNode {
public: // constructors defined here protected: T element; int next; } In chainNode the data type of next is chainNode<T>*, whereas in simulatedNode the data type of next is int. Other than this, there is really no significant difference.
5
How It All Looks c a e d b c a e d b next 11 14 14 -1 8 element 1 2 3
In chain<T> firstNode is of type chainNode<T>*, here it is an int. Logically, there is no difference between a list represented using C++ pointers/references and one represented using simulated pointers (i.e., integer pointers). next 11 14 14 -1 8 element c a e d b 1 2 3 4 5 8 11 14 firstNode = 4
6
Still Drawn The Same firstNode a b c d e -1
Note that we use a next value of –1 to mean that there is no next node. The use of –1 in our simulated pointer representations is equivalent to the use of null in the linked representations that use C++ pointers/references.
7
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 Memory management is a crucial component of any programming system that permits dynamic allocation/deallocation of memory. Linked lists require dynamic allocation/deallocation of nodes, and so require memory management methods. The term `garbage’ refers to memory that is neither in use nor in the storage pool. Garbage collection is the process of identifying garbage and hauling it to the storage pool.
8
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. The garbage collector may be invoked whenever a request for memory fails because the storage pool doesn’t have enough memory to satisfy the request. Alternatively, the collector may work in the background scouring memory for portions/nodes that are not in use and also not in the storage pool.
9
Marking Unmark all nodes (set all mark bits to false).
c a e d b firstNode 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. Each node has a mark bit that is false when the node is unmarked and true when the node is marked.
10
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 pointer variables.
11
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. Compaction is useful when the user requests nodes of different size. Following compaction you have a single large chunk of free memory and so can handle requests for large nodes.
12
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.
13
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.
14
Advantages Of Garbage Collection
Applications may run faster when run on computers that have more memory. This could happen when the garbage collector is set up to run only when a request to allocate a node cannot be met using memory currently in the storage pool. In such an implementation of garbage collection, increasing a computer’s memory could reduce the number of times the garbage collector is invoked.
15
Disadvantage Of Garbage Collection
Garbage collection time is linear in memory size (not in amount of free memory). Many different algorithms exist, tradeoffs So a lot effort could be expended to reclaim a small amount of unused memory.
16
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.
17
Advantage Of Alternative
Time to free nodes is proportional to number of nodes being freed and not to total memory size. It is desirable to spend effort in proportion to the amount of useful work that is done.
18
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.
19
Storage Pool Organization When All Nodes Have Same Size
b c d e -1 firstNode Storage pool is a chain of nodes that are not in use. Maintain a chain of free nodes Allocate from front of chain Add node that is freed to chain front
20
Simulated-Pointer Memory Management
template <class T> class simulatedSpace { public: // constructor and other methods // defined here protected: int firstNode; simulatedNode<T> *node; }
21
Constructor template <class T>
simulatedSpace (int numberOfNodes) { node = new simulatedNode<T> [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; }
22
Allocate A Node template <class T>
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;
23
Free A Node template <class T> void deallocateNode(int i)
{// Free node i. // make i first node on free space list node[i].next = firstNode; firstNode = i; }
24
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.
25
Simulated Pointers Don’t use unless you see a clear advantage to using simulated pointers over C++ pointers. Use C++11 smart pointers Reduce bugs, retain efficiency
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.