home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / pascal / 8566 < prev    next >
Encoding:
Internet Message Format  |  1993-01-27  |  2.3 KB

  1. Path: sparky!uunet!digex.com!intercon!udel!gatech!news.ans.net!cmcl2!adm!news
  2. From: tdarcos@access.digex.com (Paul Robinson)
  3. Newsgroups: comp.lang.pascal
  4. Subject: [TDR] VAX Pascal Dispose
  5. Message-ID: <35251@adm.brl.mil>
  6. Date: 27 Jan 93 16:39:15 GMT
  7. Sender: news@adm.brl.mil
  8. Lines: 81
  9.  
  10.  
  11.  F>I have a question about how can I prove or disprove that the 
  12.   >DISPOSE procedure in Vax Pascal does really return the storage 
  13.   >space pointed to by the parameter of the dispose procedure to 
  14.   >the system's free space so that it can be reused.
  15.  
  16.  F>All Comments welcome,
  17.  
  18. To do this you need to be able to "see" a pointer which you can't do
  19. because the compiler usually won't let you look at them directly, like
  20. this:
  21.  
  22. PROGRAM DUMPPTR(OUTPUT);
  23. TYPE 
  24.      ILP = ^IL;
  25.      IL = RECORD
  26.             DValue : Integer;
  27.             PREV,NEXT: ILP;
  28.           END;       
  29.     (* 1 *)      
  30.  
  31. VAR
  32.      V1,V2,V3: ILP;
  33.     (* 2 *)
  34.  
  35. begin
  36.     new(V1);
  37.     writeln(v1);
  38.     new(v2);
  39.     writeln(V2);   (* 3 *)
  40.     dispose(V2);
  41.     new(v3);
  42.     writeln(V3);    (* The second two output lines *)
  43.                     (* should be identical *)
  44. end.
  45.  
  46. This won't work because you can't write a pointer out (most Pascals won't
  47. allow it, anyway).  So you have to fool the compiler, by inserting code at
  48. points 1,2 and 3.  So we have the following:
  49.  
  50. PROGRAM ImprovedDump(OUTPUT);
  51.  
  52. TYPE  
  53.      ILP = ^IL;
  54.      IL = RECORD
  55.             DValue : Integer;
  56.             PREV,NEXT: ILP;
  57.           END;
  58.       SW = RECORD
  59.               CASE boolean OF
  60.                  true: (P: ILP);
  61.                 false: (I: INTEGER);
  62.            END;
  63.  
  64. VAR
  65.      V1,V2,V3: ILP;
  66.      T: SW;
  67.  
  68. BEGIN
  69.         new(V1);
  70.         T.P := V1;
  71.         WRITELN(T.I);
  72.         new(v2);
  73.         T.P := V2;
  74.         WRITELN(T.I);
  75.         dispose(v2);
  76.         new(v3);
  77.         T.P := V3;
  78.         WRITELN(T.I);  (* The second two output lines should be *)
  79.                        (* identical *)
  80.  end.
  81.  
  82. The construct SW and the T variable are sometimes called "pornographic"
  83. memory access because this is something Pascal provides that usually only
  84. C programmers can have: access to any memory anywhere.  By simply
  85. assigning an integer value to T.I and reading it with, say, T.P, it is
  86. possible to examine any location in memory.  This is a "kluge" to fool the
  87. compiler which doesn't know what we are trying to accomplish.
  88.  
  89.  
  90.  
  91.