home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / modula2 / 1116 < prev    next >
Encoding:
Internet Message Format  |  1992-09-07  |  9.1 KB

  1. Path: sparky!uunet!mcsun!Germany.EU.net!unidui!flyer!easix!tron.gun.de!g_dotzel
  2. From: g_dotzel@tron.gun.de (Guenter Dotzel)
  3. Newsgroups: comp.lang.modula2
  4. Subject: Re: Oberon question/pointer initialisati
  5. Message-ID: <2953C1389G@tron.gun.de>
  6. Organization: TRON Public Mailbox, Neurath, Germany
  7. Date: Sat, 5 Sep 92 10:53:00 +0200
  8. Lines: 302
  9.  
  10. JPUSSI%CS.JOENSUU.FI@USENET.ZER wrote on Tue, 09-01-1992 at 08:20 h
  11. under the subject 'Oberon question':
  12.  
  13. > In some paper I read that in Oberon, all pointer variables are
  14. > initialised to NIL. Now I would like to know if this means that
  15. > also pointer fields in dynamically (with NEW) allocated records
  16. > or in static records are initialised to NIL. 
  17. > In short are pointers always either NIL or valid pointers and
  18. > not garbage pointers. May it vary between implementations?
  19.  
  20. In our implementation (Oberon-2 for VAX/VMS), *all pointers* are set to NIL
  21.  
  22. - All static (global) variables are set to 0 (0=NIL for pointers);
  23.   this is guaranteed by a linker directive for the VMS-program loader.
  24. - All local variables of any pointer type even within any record or
  25.   array structure are cleared to NIL when entering a procedure,
  26.   although we have an implementation restriction to max. 10000 pointers
  27.   per structure. The code to clear those variables is suppressed if you 
  28.   use the /NOCHECK compilation qualifier.
  29. - Heap space allocated with NEW is cleared to 0 at run-time.
  30.   It uses VMS's LIB$GET_VM and clears the memory allocated using the MOVC5
  31.   instruction (the run-time module could be replaced by a version that 
  32.   doesn't clear for time critical applications. (since a VMS routine is used
  33.   to allocate the heap, we can use ISO Modula-2's Storage.DEALLOCATE for
  34.   disposing it (see example below).
  35.  
  36. Below are two test programs
  37.  
  38. -testnew
  39. -TestLocalPtr
  40.  
  41. which we used to check our compiler implementation.
  42.  
  43. Greetings, Guenter
  44.  
  45. MODULE testnew;
  46. (* Oberon-2: check whether heap space is cleared to  0/NIL; ok if
  47.    the program doesn't call HALT when executed.
  48.  
  49. Guenter Dotzel, ModulaWare GmbH, Wilhelmstr. 17A, D-W 8520 Erlangen/F.R.Germany
  50. Modula-2 & Oberon-2 Compiler Manufactur (VAX/VMS, OS/2 and Unix-Platforms)
  51. Tel. +49 (9131) 208395, Fax +49 (9131) 28205.
  52. E-mail/Internet: 100023.2527@compuserve.com
  53.                  g_dotzel@ame.nbg.sub.org
  54. *)
  55. IMPORT SYSTEM, Storage, CTR, 
  56.   W:= SWholeIO, T:= STextIO; (* these are the ISO Modula-2 Std I/O Lib modules*)
  57.  
  58. CONST
  59.   PtrSize = SIZE(SYSTEM.WORD);
  60.   ExtOutput = FALSE;
  61.   s1 = 199000;
  62.   s2 = 10000;
  63.  
  64. TYPE x=ARRAY s1 OF CHAR;
  65.      y=ARRAY s2 OF CHAR;
  66.      z=RECORD a, b, c, d: LONGINT; END;
  67.  
  68.      rp = POINTER TO z;
  69.  
  70. VAR 
  71.   p: POINTER TO x; 
  72.   q: POINTER TO y;
  73.  
  74.   r: rp;
  75.   r1: POINTER TO RECORD a, b, c, d: LONGINT; END;
  76.   r2: POINTER TO z;
  77.  
  78.   i: LONGINT;
  79.   a: CTR.ADDRESS;
  80.   last: LONGINT;
  81.  
  82. BEGIN
  83.   NEW(p); last:= SYSTEM.VAL(LONGINT, p);
  84.   FOR i:= 0 TO s1 - 1 DO
  85.     p^[i]:= CHR(i MOD 256);
  86.   END;
  87.   a:= SYSTEM.VAL(CTR.ADDRESS, p);
  88.   Storage.DEALLOCATE(a, SIZE(x));
  89.   NEW(p);
  90.   IF last # SYSTEM.VAL(LONGINT, p) THEN
  91.     T.WriteString('"p" not reallocated at the same address'); T.WriteLn;
  92.     HALT(21);
  93.   END;
  94.   IF ExtOutput THEN
  95.     T.WriteString('test big array contents'); T.WriteLn; T.WriteLn;
  96.     T.WriteString('   index,    value'); T.WriteLn;
  97.   END;
  98.   FOR i:= 0 TO s1 - 1 DO
  99.     IF p^[i] # CHR(0) THEN 
  100.       IF ExtOutput THEN
  101.         W.WriteInt(i, 8);
  102.         W.WriteInt(ORD(p^[i]), 10); T.WriteLn;
  103.       ELSE
  104.         T.WriteString('big-array test failed at index '); W.WriteInt(i, 0);
  105.         T.WriteLn;
  106.         HALT(21);
  107.       END;
  108.     END;
  109.   END;
  110.  
  111.   NEW(q); last:= SYSTEM.VAL(LONGINT, q);
  112.   FOR i:= 0 TO s2 - 1 DO
  113.     q^[i]:= CHR(i MOD 256);
  114.   END;
  115.   a:= SYSTEM.VAL(CTR.ADDRESS, q);
  116.   Storage.DEALLOCATE(a, SIZE(y));
  117.   NEW(q);
  118.   IF last # SYSTEM.VAL(LONGINT, q) THEN
  119.     T.WriteString('"q" not reallocated at the same address'); T.WriteLn;
  120.     HALT(21);
  121.   END;
  122.   FOR i:= 0 TO s2 - 1 DO
  123.     IF q^[i] # CHR(0) THEN
  124.       T.WriteString('small-array test failed at index '); W.WriteInt(i, 0);
  125.       T.WriteLn;
  126.       HALT(31);
  127.     END;
  128.   END;
  129.  
  130.  
  131.   NEW(r);  r^.a:= -1;   r^.b:= -2;   r^.c:= -3;   r^.d:= -4;
  132.   last:= SYSTEM.VAL(LONGINT, r);
  133.   IF ExtOutput THEN
  134.     NEW(r1); r1^.a:= -1;  r1^.b:= -2;  r1^.c:= -3;  r1^.d:= -4;
  135.     NEW(r2); r2^.a:= -1;  r2^.b:= -2;  r2^.c:= -3;  r2^.d:= -4;
  136.  
  137.     T.WriteString('r        '); 
  138.     W.WriteInt(SYSTEM.VAL(LONGINT, r), 0); T.WriteLn;
  139.     T.WriteString('ADR(r^  )'); 
  140.     W.WriteInt(SYSTEM.VAL(LONGINT, SYSTEM.ADR(r^)), 0); T.WriteLn;
  141.     T.WriteString('ADR(r^.a)'); 
  142.     W.WriteInt(SYSTEM.VAL(LONGINT, SYSTEM.ADR(r^.a)), 0); T.WriteLn;
  143.     T.WriteString('ADR(r^.b)'); 
  144.     W.WriteInt(SYSTEM.VAL(LONGINT, SYSTEM.ADR(r^.b)), 0); T.WriteLn;
  145.     T.WriteString('ADR(r^.c)'); 
  146.     W.WriteInt(SYSTEM.VAL(LONGINT, SYSTEM.ADR(r^.c)), 0); T.WriteLn;
  147.     T.WriteString('ADR(r^.d)'); 
  148.     W.WriteInt(SYSTEM.VAL(LONGINT, SYSTEM.ADR(r^.d)), 0); T.WriteLn;
  149.   END;
  150.  
  151.   a:= SYSTEM.VAL(CTR.ADDRESS, SYSTEM.VAL(LONGINT, r) - PtrSize);
  152.   Storage.DEALLOCATE (a, SIZE(z) + PtrSize); (*adjust for type descriptor*)
  153.  
  154.   NEW(r);
  155.   IF last # SYSTEM.VAL(LONGINT, r) THEN
  156.     T.WriteString('"r" not reallocated at the same address'); T.WriteLn;
  157.     HALT(21);
  158.   END;
  159.   IF r^.a # 0 THEN 
  160.     T.WriteString('record test failed at field "a"'); T.WriteLn;
  161.     HALT(41); 
  162.   END;
  163.   IF r^.b # 0 THEN 
  164.     T.WriteString('record test failed at field "b"'); T.WriteLn;
  165.     HALT(41); 
  166.   END;
  167.   IF r^.c # 0 THEN 
  168.     T.WriteString('record test failed at field "c"'); T.WriteLn;
  169.     HALT(41); 
  170.   END;
  171.   IF r^.d # 0 THEN 
  172.     T.WriteString('record test failed at field "d"'); T.WriteLn;
  173.     HALT(41); 
  174.   END;
  175. END testnew.
  176. MODULE CTR;
  177. (* central definition of INTEGER and CARDINAL *)
  178. IMPORT SYSTEM;
  179.  
  180. TYPE 
  181.   INTEGER* = LONGINT; (* 32 Bit signed whole number type *)
  182.   CARDINAL* = LONGINT;
  183.   ADDRESS* = LONGINT; (* SYSTEM.ADR() is of LONGINT type! *)
  184.  
  185. (* select your enumeration types carefully: *)
  186.   ENUM8* = SHORTINT; (* ORD(MAX(corresponding_Modula_2_enum) < 256 *)
  187.   ENUM16* = INTEGER; (* ORD(MAX(corresponding_Modula_2_enum) < 65536 *)
  188.   ENUM32* = LONGINT; (* ORD(MAX(corresponding_Modula_2_enum) < 2^31 :-) *)
  189.  
  190. (* WORD and QUDWORD are extension of the Oberon-2 module SYSTEM:
  191.    Sorry, there was no other way! *)
  192.  
  193.   WORD* = SYSTEM.WORD; (* 32 Bit *)
  194.   (* WORD is used in ConvType only (no recursive procedure definitions) *)
  195.  
  196.   QUADWORD* = SYSTEM.QUADWORD; (* 64 Bit *)
  197.   (* QUADWORD is used in RndFile only (no structured function values) *)
  198.  
  199. END CTR.
  200.  
  201. MODULE TestLocalPtr;
  202. (* Oberon-2: tests whether all ptrs are cleared to nil at startup 
  203.   and procedure entry.
  204.  
  205. Guenter Dotzel, ModulaWare GmbH, Wilhelmstr. 17A, D-W 8520 Erlangen/F.R.Germany
  206. Modula-2 & Oberon-2 Compiler Manufactur (VAX/VMS, OS/2 and Unix-Platforms)
  207. Tel. +49 (9131) 208395, Fax +49 (9131) 28205.
  208. E-mail/Internet: 100023.2527@compuserve.com
  209.                  g_dotzel@ame.nbg.sub.org
  210.  
  211. EB/04-Sep-1992
  212. *)
  213.  
  214.  
  215. TYPE
  216.   ptr = POINTER TO structure;
  217.  
  218.   structure = RECORD
  219.     p1, p2, p3: ptr;
  220.   END;
  221.  
  222. VAR
  223.   s: structure;
  224.   garbage: ARRAY 20000 OF CHAR;
  225.   i: LONGINT;
  226.  
  227. PROCEDURE x;
  228.  
  229. TYPE
  230.   extension = RECORD
  231.     (structure)
  232.     p4, p5, p6: ptr;
  233.     k: LONGINT;
  234.     c: CHAR;
  235.   END;
  236.   ptrarray = ARRAY 200 OF ptr;
  237.  
  238. VAR
  239.     test1: INTEGER;
  240.   s: structure;
  241.     test5: INTEGER;
  242.   e: extension;
  243.     test2: INTEGER;
  244.   l1, l2, l3: ptr;
  245.     test6: INTEGER;
  246.   q: ptrarray;
  247.     test3: INTEGER;
  248.   r: RECORD
  249.     p1, p2, p3, p4, p5,
  250.     p6, p7, p8, p9, p0 : ptr;
  251.   END;
  252.     test4: INTEGER;
  253.   pa: POINTER TO ARRAY 10 OF CHAR;
  254.   i: INTEGER;
  255.  
  256. BEGIN
  257.   IF test1 = 0 THEN     HALT(99); END;  (* stack must be non zero *)
  258.   IF test2 = 0 THEN     HALT(99); END;  (* stack must be non zero *)
  259.   IF test3 = 0 THEN     HALT(99); END;  (* stack must be non zero *)
  260.   IF test4 = 0 THEN     HALT(99); END;  (* stack must be non zero *)
  261.   IF test5 = 0 THEN     HALT(99); END;  (* stack must be non zero *)
  262.   IF test6 = 0 THEN     HALT(99); END;  (* stack must be non zero *)
  263.  
  264.   IF s.p1   # NIL THEN HALT(31); END;
  265.   IF s.p2   # NIL THEN HALT(32); END;
  266.   IF s.p3   # NIL THEN HALT(33); END;
  267.   IF e.p1   # NIL THEN HALT(41); END;
  268.   IF e.p2   # NIL THEN HALT(42); END;
  269.   IF e.p3   # NIL THEN HALT(43); END;
  270.   IF e.p4   # NIL THEN HALT(44); END;
  271.   IF e.p5   # NIL THEN HALT(45); END;
  272.   IF e.p6   # NIL THEN HALT(46); END;
  273.   IF l1     # NIL THEN HALT(51); END;
  274.   IF l2     # NIL THEN HALT(51); END;
  275.   IF l3     # NIL THEN HALT(51); END;
  276.  
  277.   FOR i:= 0 TO 199 DO 
  278.     IF q[i] # NIL THEN HALT(61); END; 
  279.   END;
  280.  
  281.   IF r.p1   # NIL THEN HALT(71); END;
  282.   IF r.p2   # NIL THEN HALT(72); END;
  283.   IF r.p3   # NIL THEN HALT(73); END;
  284.   IF r.p4   # NIL THEN HALT(74); END;
  285.   IF r.p5   # NIL THEN HALT(75); END;
  286.   IF r.p6   # NIL THEN HALT(76); END;
  287.   IF r.p7   # NIL THEN HALT(77); END;
  288.   IF r.p8   # NIL THEN HALT(78); END;
  289.   IF r.p9   # NIL THEN HALT(79); END;
  290.   IF r.p0   # NIL THEN HALT(80); END;
  291.  
  292.   IF pa     # NIL THEN HALT(91); END;
  293. END x;
  294.  
  295. PROCEDURE g(a: ARRAY OF CHAR; VAR b: ARRAY OF CHAR);
  296. BEGIN a[0]:= 'A'; b[0]:= 'B'; INC(i);
  297.   IF i < 10 THEN g(b, a); x; (* recurse 10 times on stack *)
  298.   END;
  299. END g;
  300.  
  301. BEGIN
  302.   FOR i:= 0 TO 19999 DO garbage[i]:= CHR(i MOD 254 + 1); END; (* non zero *)
  303.   IF s.p1 # NIL THEN HALT(21); END;
  304.   IF s.p2 # NIL THEN HALT(22); END;
  305.   IF s.p3 # NIL THEN HALT(23); END;
  306.   i:= 0;
  307.   g(garbage, garbage); (* create garbage on stack *)
  308.   x;
  309. END TestLocalPtr.
  310.  
  311.