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 213<br />

One way of manag<strong>in</strong>g dynamically allocated storage is to form a daisy cha<strong>in</strong> of the allocated storage. The<br />

usual approach when implement<strong>in</strong>g this technique is to <strong>in</strong>clude with the record component a value which can hold<br />

the access value of the next item <strong>in</strong> the daisy cha<strong>in</strong>. The end of the cha<strong>in</strong> is <strong>in</strong>dicated by the value null. The<br />

value null is special as it is considered the null value for any access type. The <strong>Ada</strong> system will guarantee that no<br />

allocated access value can ever have the value null.<br />

The code below forms a daisy cha<strong>in</strong> of two items of storage which represent <strong>in</strong>dividual people.<br />

declare<br />

Max_Chs : constant := 7;<br />

type Gender is ( Female, Male );<br />

type Height_Cm is range 0 .. 300;<br />

type Person;<br />

--Incomplete declaration<br />

type P_Person is access Person; --Access type<br />

type Person is record<br />

Name : Str<strong>in</strong>g( 1 .. Max_Chs ); --Name as a Str<strong>in</strong>g<br />

Height : Height_Cm := 0; --Height <strong>in</strong> cm.<br />

Sex : Gender; --Gender of person<br />

Next : P_Person;<br />

end record;<br />

People : P_Person;<br />

beg<strong>in</strong><br />

People := new Person'("Mike ", 183, Male, null );<br />

People.Next := new Person'("Cor<strong>in</strong>na", 171, Female, null);<br />

end;<br />

Note: P_Person and Person are mutually dependent upon each other as both conta<strong>in</strong> a reference to each<br />

other. To fit <strong>in</strong> with the rule that all items must be declared before they can be used, <strong>Ada</strong> <strong>in</strong>troduces the<br />

concept of a tentative declaration. This is used when Person is def<strong>in</strong>ed as 'type Person;'. The<br />

full declaration of person is def<strong>in</strong>ed a few l<strong>in</strong>es further down.<br />

The resultant data structure is illustrated <strong>in</strong> Figure 15.3.<br />

People<br />

Mike 183 Male<br />

Cor<strong>in</strong>na 171 Female null<br />

Figure 15.3 Daisy cha<strong>in</strong> of two people.<br />

In the above code the daisy cha<strong>in</strong> of people could have been created with the s<strong>in</strong>gle assignment statement:<br />

People := new Person'("Mike ", 183, Male,<br />

new Person'("Cor<strong>in</strong>na", 171, Female, null) );<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!