18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dynamic memory allocation 217<br />

The package body is as follows:<br />

with Unchecked_Deallocation;<br />

pragma Elaborate_All( Unchecked_Deallocation );<br />

package body Class_Stack is<br />

procedure Dispose is<br />

new Unchecked_Deallocation( Node, P_Node );<br />

The procedure Dispose is an <strong>in</strong>stantiation of the generic package Unchecked_Deallocation that<br />

returns space back to the heap. The parameters to the generic package Unchecked_Deallocation are of two<br />

types: firstly, the type of the object to be disposed, and secondly, the access type for this object.<br />

Note: This procedure does little error check<strong>in</strong>g. It is important not to dispose of storage which is still active.<br />

The empty list is represented by the p_head conta<strong>in</strong><strong>in</strong>g the null po<strong>in</strong>ter. The null po<strong>in</strong>ter is used to<br />

<strong>in</strong>dicate that currently the object P_Head does not po<strong>in</strong>t to any storage. This can be imag<strong>in</strong>ed as Figure 15.4.<br />

P_Head<br />

Figure 15.4 A location conta<strong>in</strong><strong>in</strong>g the null access value or po<strong>in</strong>ter.<br />

When an item (<strong>in</strong> this case an Integer) has been added to the stack it will look like Figure 15.5.<br />

P_Head<br />

3<br />

Value P_Next<br />

Figure 15.5 A stack conta<strong>in</strong><strong>in</strong>g one element.<br />

To access the component value the . operator is used.<br />

Note:<br />

The compiler will generate the appropriate code to reference value. In this case it will <strong>in</strong>volve a dereferenc<strong>in</strong>g<br />

through the po<strong>in</strong>ter held <strong>in</strong> P_Head.<br />

P_Head.Item = 3;<br />

The function Push creates a new element and cha<strong>in</strong>s this <strong>in</strong>to a l<strong>in</strong>ked list of elements which hold the items<br />

pushed onto the stack.<br />

The cha<strong>in</strong> of elements after add<strong>in</strong>g 3 and<br />

2 to an <strong>in</strong>stance of an Integer Stack<br />

The cha<strong>in</strong> of elements after push<strong>in</strong>g 1 on<br />

to an <strong>in</strong>stance of an Integer Stack<br />

P_Head<br />

P_Head<br />

1<br />

2<br />

2<br />

3<br />

3<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!