home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / ada / 2493 < prev    next >
Encoding:
Internet Message Format  |  1992-08-31  |  5.3 KB

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