home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / ada / 2492 < prev    next >
Encoding:
Text File  |  1992-08-31  |  5.5 KB  |  109 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!haven.umd.edu!darwin.sura.net!europa.asd.contel.com!news.ans.net!cmcl2!CS.NYU.EDU!figueroa
  3. From: figueroa@CS.NYU.EDU (Samuel A. Figueroa)
  4. Subject: Message from Dewar re: Address Clauses
  5. Message-ID: <9208311626.AA23212@cs.NYU.EDU>
  6. Sender: daemon@cmcl2.nyu.edu (Mr Background)
  7. Organization: New York University
  8. Date: Mon, 31 Aug 1992 16:26:36 GMT
  9. Lines: 98
  10.  
  11. Date: Mon, 31 Aug 92 07:13:31 -0400
  12. From: dewar (Robert Dewar)
  13. Message-Id: <9208311113.AA21505@cs.NYU.EDU>
  14. Subject: Could you forward this to Comp.Lang.Ada
  15.  
  16. Andrew Dunstan writes:
  17.  "Thanks for pointing this [the rule in 13.5(8)] out - I did actually look
  18.   in the LRM before I posted, but obviously not hard enough! Finding these
  19.   little things in the LRM is like looking for a needle in a haystack."
  20.  
  21. Well the RM takes many knocks, some deserved, but really! this is somewhat
  22. unfair. If I was wondering about the use of an address clause in Ada, I
  23. should think I would read section 13.5 of the reference manual, whose
  24. title is, reasonably helpfully "Address Clauses". Since this section is
  25. quite short (only 21 lines long plus a short example and a note), it would
  26. seem quite reasonable to read the full 21 lines. 3 of these lines (a
  27. separate paragraph, para (5), contain the restriction stated pretty clearly:
  28.  
  29.   "Address clauses should not be used to achieve overlays of objects or
  30.    overlays of program units. Nor should a given interrupt be linked to more 
  31.    than one entry. Any program using address clauses to achieve such an 
  32.    effect is erroneous."
  33.  
  34. So, clear enough. But perhaps it is worth taking a moment to discuss WHY
  35. this restriction is present. There are really two cases. First, the case
  36. where the address clause is used to give two names of exactly the same type
  37. to the same object, something like:
  38.  
  39.     A : Integer;
  40.     B : Integer;
  41.     for B use at A'Address;
  42.  
  43. Now of course one might ask what on earth this is good for (if the intent is
  44. simply to give two names to the same thing, please use RENAME!) but on the
  45. other hand, one could take the attitude: sure it's not much use, but why
  46. shouldn't it work? This is one of those interesting cases where the rationale
  47. is not immediately obvious. The trouble is that the expression in the address
  48. clause is not required to be static (quite useful in mapping objects to places
  49. not known till execution time). This means we can have:
  50.  
  51.     A : Integer;
  52.     B : Integer;
  53.     for B use at Complex_Function (A'Address, ... );
  54.  
  55. Now the compiler has no way of knowing if a reference to B, or worse still,
  56. a reference to a function which can see B, does horrible things to arbitrary
  57. local variables. Essentially we have provided a critical leak to the 
  58. otherwise firm principle that specifically declared variables are never
  59. implicitly aliased. This would complicate optimizers, and reduce their
  60. effectiveness for no apparent significant functional gain.
  61.  
  62. [Side note: in Ada/9X, as proposed, we can take the address of a declared
  63.  variable, but the variable must be specifically marked, using ALIASED, to
  64.  warn the compiler optimizer that this kind of aliasing may occur. Presumably
  65.  we could allow the above declarations in Ada/9X if A were marked aliased, but
  66.  it hardly seems worth the effort!]
  67.  
  68. Back to the restriction, and the more significant application, which is to
  69. get the effect of REDEFINES in COBOL or EQUIVALENCE in FORTRAN. It is clear
  70. that such usage is bound in general to have an implementation dependent
  71. effect. Even in the apparently harmless case involving a variable and 
  72. a constant of the same type, an implementation may store constants and
  73. variables in different formats, different memory hierarchies etc. [someone
  74. already pointed out the ROM possibility].
  75.  
  76. Now when it comes to language constructs that are said to be implementation
  77. dependent, the RM divides them up into three categories:
  78.  
  79.   Implementation dependent, documentation in appendix F required
  80.   Implementation dependent, documentation not required
  81.   Erroneous
  82.  
  83. >From a legalistic point of view, the last two possibilities are exactly the
  84. same, since they both give the implementation total freedom to do whatever
  85. it wants, without telling you what it is doing. On the other hand, the
  86. distinction is significant, because it expresses a point of view. ID is
  87. respectable, it makes your program implementation dependent, and thus
  88. potentially non-portable, but not immoral. Erroneous on the other hand,
  89. expresses the point of view that you really shouldn't be doing this, and
  90. advises implementors that it would be in poor taste to document what they
  91. do in erroneous situations and encourage programmers to make use of the
  92. information.
  93.  
  94. So, what do you think about the choice in the case of address clauses used
  95. for overlay? Well I think the RM probably made the right choice -- this
  96. really isn't the sort of programming style that fits the Ada mold?
  97.  
  98. One final note, back to the original problem. The best general solution is
  99. to use access STRING for these kind of situations. Actually access STRING
  100. is a pretty reasonable type for the manipulation of variable length strings.
  101. Of course it means using the heap which you might want to avoid, but in that
  102. case, worry a little about using unconstrained returned values from functions
  103. at all -- many, but not all, implementations secretly use the heap anyway
  104. for this situation. So if you are writing portable code which wants to avoid
  105. using the heap, better to use an interface like GET_LINE, even if it is
  106. clunkier!
  107.  
  108.     Robert B. K. Dewar (that's quite enough signature for me, thanks!)
  109.