home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap13.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  16.7 KB  |  410 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 13
  5.                                          THE ACCESS TYPE VARIABLE
  6.  
  7.  
  8. THE ACCESS TYPE IS DIFFERENT
  9. _________________________________________________________________
  10.  
  11. The access type variable is different from every  ===============
  12. other type we have encountered because it is not    ACCESS1.ADA
  13. actually a variable which can store a piece of    ===============
  14. data, but contains the address of another piece
  15. of data which can be manipulated.  As always the
  16. best teacher is an example, so examine the file named ACCESS1.ADA
  17. for an example program with a few access type variables in it.
  18.  
  19.  
  20.  
  21. DECLARING AN ACCESS TYPE VARIABLE
  22. _________________________________________________________________
  23.  
  24. In line 10 we declare a new type, an access type.  As with all
  25. types, the reserved word type is given, followed by the type name,
  26. then the reserved words is and the type definition.  The type
  27. definition begins with the reserved word access, which denotes an
  28. access type variable, then by the type which we wish to access.
  29. The type which we wish to access can be any type which has been
  30. declared prior to this point in the program, either a predeclared
  31. type or a type we have declared.  Because there are no predeclared
  32. access types available in Ada, we must declare all access types we
  33. wish to use.
  34.  
  35. The new type is used to declare three access variables in line 11.
  36. No actual variables are declared, only access to three places in
  37. memory which actually do not exist yet.  The access type variables
  38. do not store an integer value, as might be expected, but instead
  39. store the address of an integer value located somewhere within the
  40. address space of the computer memory.
  41. Figure 13-1 illustrates graphically the condition of the system at
  42. this point.  A box with a dot in the center depicts an access
  43. variable and an empty box will be used to depict a scalar variable.
  44.  
  45.  
  46.  
  47. WE NEED SOME DATA TO POINT AT
  48. _________________________________________________________________
  49.  
  50. As we begin the executable part of the program, we have no data to
  51. access, so we create some data storage in line 14 using the new
  52. reserved word.  This tells the system to go somewhere and create
  53. a variable of type INTEGER, with no name, and cause the access
  54. variable named Index to point at this new variable.  The effective
  55. address of the new variable is assigned to the access variable
  56. Index, but the new variable still has no assigned value.
  57.  
  58.                                                         Page 13-1
  59.  
  60.                             Chapter 13 - The Access Type Variable
  61.  
  62.  
  63. Line 15 tells the system to assign the value of 13 to the new
  64. variable by using a very strange looking method of doing so.  For
  65. the first two example programs in this chapter, we will simply say
  66. that the value of all of the variable is set to the indicated
  67. value, namely 13.  The end result is that the access variable named
  68. Index is pointing someplace in memory which has no name, but
  69. contains the value of 13.  Figure 13-2 illustrates our current
  70. situation.
  71.  
  72. Lines 16 through 18 indicate that it is possible to display the
  73. value of this variable using the same method we have used in all
  74. earlier lessons of this tutorial.  If you remember to add the .all
  75. to the access variable, you will be referring to the data stored
  76. at the location which it accesses.
  77.  
  78. Line 20 is used to create another variable of type INTEGER
  79. somewhere in memory, with Arrow pointing to it, but which contains
  80. no value as yet.  The next line says to take the value that is
  81. stored at the location to which Index points, add 16 to it, and
  82. store the result, which should be 29, in the location to which
  83. Arrow points.  Arrow actually "accesses" the variable, but it is
  84. a bit more descriptive to use the word points, especially if you
  85. are a Pascal or C programmer.
  86.  
  87.  
  88.  
  89. THE THIRD ACCESS VARIABLE
  90. _________________________________________________________________
  91.  
  92. We have not yet used the access variable named There, so we
  93. instruct the system, in line 22, to cause it to point to the same
  94. piece of data which Arrow is currently accessing.  By failing to
  95. add the all to the two access variables, we are assigning the
  96. access address to There rather than the value which Arrow accesses.
  97. If only one of them had the all appended in line 22, there would
  98. be a type clash resulting in a compiler error message.  All three
  99. access variables are accessing some data somewhere, so we can use
  100. their .all notation to display all three values.  See figure 13-3
  101. for a graphic representation of the current data space.
  102.  
  103. Note that there are actually only two variables, because two of
  104. the access variables are pointing at the same piece of data.  This
  105. is illustrated when one of the variables is changed in line 29, and
  106. when the program is executed, two of the values are different.
  107.  
  108. Be sure to compile and run this program.  When you think you
  109. understand it, see if you can modify it such that all three access
  110. variables point to the same piece of data.
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.                                                         Page 13-2
  118.  
  119.                             Chapter 13 - The Access Type Variable
  120.  
  121. ACCESSING INTEGER AND FLOAT TYPE VARIABLES
  122. _________________________________________________________________
  123.  
  124. Examine the file named ACCESS2.ADA for some       ===============
  125. additional examples of access type variables.       ACCESS2.ADA
  126. We begin by declaring two access variables which  ===============
  127. access INTEGER type variables and three access
  128. variables that access FLOAT type variables.  It
  129. should be pointed out, and it probably comes as no surprise to you,
  130. that it is illegal to attempt to access a variable with the wrong
  131. type of access variable.  Explicit type conversion is possible
  132. concerning the data types, but not the access types.
  133.  
  134. Line 19 introduces a new construct, that of initializing a variable
  135. when it is created.  Using a form similar to qualification, an
  136. INTEGER type variable is created somewhere in memory, initialized
  137. to 173, and the access variable named Index is assigned its address
  138. so that it points to it, or accesses it.  After executing line 20,
  139. the data space is as shown in figure 13-4.
  140.  
  141. Be sure to note the difference between the expressions in lines 26
  142. and 27.  In line 26, the value stored at the place where Index
  143. points, is stored at the place where Arrow points.  However, in
  144. line 27, the access variable Index is caused to point to the same
  145. location where the access variable Arrow points.
  146.  
  147.  
  148.  
  149. A FLOAT TYPE ACCESS VARIABLE
  150. _________________________________________________________________
  151.  
  152. Line 29 illustrates creation of a FLOAT type variable initialized
  153. with the value of Pi.  Since the access variable names are used in
  154. lines 30 and 31 without the all appended, all three FLOAT type
  155. access variables are assigned to access the same variable, and some
  156. results are displayed.  Figure 13-5 illustrates the condition of
  157. the system at this point.
  158.  
  159. The single FLOAT type variable is doubled in line 38, and it is
  160. displayed again three different ways.  Be sure to compile and
  161. execute this program.
  162.  
  163.  
  164.  
  165. ACCESSING A RECORD VARIABLE
  166. _________________________________________________________________
  167.  
  168. Examine the example program named ACCESS3.ADA     ===============
  169. for some additional uses for access variables.      ACCESS3.ADA
  170. This program begins by defining a record type,    ===============
  171. then an access type which can be used to access
  172. data of this record type.  Ignore the procedure
  173. Free in line 19 for a short time.  In line 22, we declare a
  174. variable named Myself which is an access variable that accesses a
  175.  
  176.                                                         Page 13-3
  177.  
  178.                             Chapter 13 - The Access Type Variable
  179.  
  180. variable of the type MY_RECORD.  Since the record does not exist
  181. yet, the access variable is actually pointing nowhere.  According
  182. to the Ada definition, the created access variable will be
  183. initialized to the value null, which means it points nowhere.  This
  184. value can be tested for as we shall see later.  All access
  185. variables used in an Ada program, regardless of how they are
  186. declared, will be initially assigned the value of null.  This is
  187. true, unless they are specifically initialized to some value as
  188. shown in line 23.
  189.  
  190. Line 23 is very interesting because we declare an access variable
  191. named Friend, and initialize it by creating a new record somewhere
  192. in memory, then initializing the record itself to the values given
  193. in the positional aggregate.  Finally, the access variable Friend
  194. is caused to point to the newly created record.  It is permissible
  195. to create a new record, but omit the initialization, supplying the
  196. initial values in the executable part of the program.  We finally
  197. declare a BOOLEAN type variable for later use.  Figure 13-6
  198. illustrates our current data space.
  199.  
  200.  
  201.  
  202. USING THE RECORD ACCESS VARIABLE
  203. _________________________________________________________________
  204.  
  205. In line 29, we create a new variable of type MY_RECORD, which is
  206. composed of three separate fields.  The three fields are assigned
  207. in much the same manner that they were assigned in the chapter
  208. where we studied records, so this should pose no problem for you.
  209. In line 35, we create another new record somewhere and initialize
  210. it to the values given, and cause the variable named Friend to
  211. point to it, or access it.  See figure 13-7.
  212.  
  213. It should be pointed out that the only data which can be accessed
  214. with an access variable are those that are created with the new
  215. function.  It is not possible to get the address of a statically
  216. declared variable and assign that address to an access variable.
  217. This is possible in both Pascal and C, but not in Ada.
  218.  
  219.  
  220.  
  221. NOW WE HAVE SOME LOST VARIABLES
  222. _________________________________________________________________
  223.  
  224. Consider that the access variable Friend already had some data that
  225. it was pointing at, and we told the system to cause it to point at
  226. this newly created record.  The record it was formerly pointing at
  227. is now somewhere in memory, but has nothing pointing at it, so it
  228. is in effect lost.  We cannot store anything in it, nor can we read
  229. out the data that is stored in it.  Of even more consequence, we
  230. cannot free up those memory locations for further use, so the space
  231. is totally lost to our program.  Of course, the operating system
  232. will take care of cleaning up all of the lost variables when our
  233. program terminates, so the data is not lost forever.  It is up to
  234.  
  235.                                                         Page 13-4
  236.  
  237.                             Chapter 13 - The Access Type Variable
  238.  
  239. us to see that we do not lose memory space through clumsy
  240. programming because Ada cannot check to see that we have reassigned
  241. an access variable.
  242.  
  243. The next interesting thing is illustrated in line 43 where all of
  244. the fields of the record which Myself accesses are assigned to all
  245. of the fields of the record which Friend accesses.  Now it makes
  246. sense why the designers of Ada chose to refer to the data which is
  247. accessed by the .all notation, it refers to all of the data that
  248. the access variable points to.  It should be pointed out that as
  249. you gain experience with Ada you will find that nearly all access
  250. type variables are used to access records, and few, if any, will
  251. access scalar variables.
  252.  
  253.  
  254.  
  255. BOOLEAN OPERATIONS WITH ACCESS VARIABLES
  256. _________________________________________________________________
  257.  
  258. Records accessed by access variables can be compared for equality
  259. or inequality, just like regular records, and they are equal only
  260. if all fields in one record are equal to the corresponding fields
  261. in the other record.  Line 45 will therefore result in TRUE,
  262. because the records are identical, due to the assignment in line
  263. 43.  Access variables can also be compared to each other, and
  264. result in TRUE only if they are both pointing to the same object.
  265. In line 46, the result is FALSE because they are pointing to
  266. different records, even though the records they point to happen to
  267. be equal to each other.
  268.  
  269.  
  270.  
  271. WHAT IS UNCHECKED DEALLOCATION?
  272. _________________________________________________________________
  273.  
  274. The procedure Unchecked_Deallocation is a required part of Ada so
  275. your compiler writer has supplied you with this procedure as a part
  276. of the library.  Any dynamically allocated data can be freed up for
  277. reuse by the system through use of this procedure as illustrated
  278. in this program.  You must first instantiate a copy of the generic
  279. procedure as illustrated in line 19, and name it any available
  280. identifier you choose.  You must supply two types as parameters,
  281. the first being the object type you wish to deallocate, and the
  282. second being the access type.  The name Free is generally used
  283. because that name is used for the equivalent procedure in Pascal
  284. and in C.
  285.  
  286. To actually deallocate some storage, you use the name of the access
  287. variable which is accessing the storage to be released as the only
  288. parameter of the procedure named Free as illustrated in lines 49
  289. and 50.
  290.  
  291. There is a lot more to be said about deallocation of storage and
  292. the way it is accomplished, but the details will be left until
  293.  
  294.                                                         Page 13-5
  295.  
  296.                             Chapter 13 - The Access Type Variable
  297.  
  298. chapter 23, after you gain more experience with Ada.  Until then,
  299. with your limited knowledge of Ada, you will probably not be
  300. writing programs in which you will need this information.  Be sure
  301. to compile and execute this program.
  302.  
  303.  
  304.  
  305. GARBAGE COLLECTION
  306. _________________________________________________________________
  307.  
  308. Another way to deallocate the data accessed by the access
  309. variables, is to assign the value of null to the access variables.
  310. This will cause the dynamically allocated variables to have no
  311. access variables accessing them, and they are then unusable, or
  312. garbage.  An Ada implementation may implement a garbage collector
  313. to search for un-accessed data and reclaim the storage for further
  314. use.  Implementation of a garbage collector is optional according
  315. to the LRM.  Much more will be said about deallocation and garbage
  316. collection in chapter 23.
  317.  
  318.  
  319.  
  320. ACCESSING AN ARRAY OF DATA
  321. _________________________________________________________________
  322.  
  323. The example program named ACCESS4.ADA gives an    ===============
  324. example of using an access variable to access an    ACCESS4.ADA
  325. array.  The only thing that could be considered   ===============
  326. new here is the assignment in line 23 where the
  327. value of the array List_Of_Stuff, is assigned to
  328. the variable which is accessed by the access variable There.  Note
  329. that 6 INTEGER type values are actually assigned in this one
  330. statement.
  331.  
  332. Note that Unchecked_Deallocation is illustrated here also as an
  333. example.  The program should be simple for you to follow, and after
  334. you understand it, compile and execute it.
  335.  
  336.  
  337.  
  338. AN ARRAY OF ACCESS VARIABLES
  339. _________________________________________________________________
  340.  
  341. Examine the program ACCESS5.ADA for an example    ===============
  342. including an array of access variables which is     ACCESS5.ADA
  343. declared in line 20.  The variable named Class    ===============
  344. is composed of a total of ten access type
  345. variables, any of which can be used to point to
  346. a variable of type MY_RECORD.  The loop in lines 29 through 32 is
  347. used to first create a record variable, then assign values to the
  348. fields of the created record variable, for each of the ten access
  349. variables.  Since each record is composed of three subfields, a
  350. total of 30 separate variables are created and assigned values in
  351. this loop.  A few of the variables are reassigned values in lines
  352.  
  353.                                                         Page 13-6
  354.  
  355.                             Chapter 13 - The Access Type Variable
  356.  
  357. 34 through 40 for illustrative purposes, and the entire array named
  358. Class is displayed on the monitor.
  359.  
  360. Compile and run this program, and be sure you understand the
  361. resulting printout.  The access variable will be very important
  362. when we study some of the advanced programming techniques later in
  363. this tutorial.
  364.  
  365.  
  366.  
  367. PROGRAMMING EXERCISES
  368. _________________________________________________________________
  369.  
  370. 1.   Declare a record named BOX_TYPE composed of three FLOAT type
  371.      elements, Length, Width, and Height.  Use the new function to
  372.      create three boxes named Small, Medium, and Large, and store
  373.      suitable values in all nine fields.  Display the data
  374.      concerning the three boxes along with the volume of each box.
  375.  
  376. 2.   Add the Unchecked_Deallocation procedure to the other three
  377.      example programs, ACCESS1.ADA, ACCESS2.ADA, and ACCESS5.ADA,
  378.      and deallocate all allocated variables.
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.                                                         Page 13-7