[comp.lang.modula2] Problem

peterhi@syma.sussex.ac.uk (Peter Hickman) (01/31/91)

I've got this problem with my Metrowerks Start Pak for the Macintosh, other
than the one I mentioned before. The example program below works fine, not the
best code in the world but then I am just a beginner.

MODULE test;

    FROM Storage IMPORT ALLOCATE, DEALLOCATE;
    FROM InOut   IMPORT Write, WriteLn, WriteCard, WriteString, HoldScreen;

    CONST
        Start = 1;
        Finish = 10;

    TYPE
        String = ARRAY [0..1] OF CHAR;
        PtEl = POINTER TO Element;
        Element = RECORD
            Name : String;
            Rank : CARDINAL;
            Next : PtEl;
        END;

    VAR
        First : PtEl;
        i : CARDINAL;

    PROCEDURE Insert( Rank : CARDINAL );

        VAR
            Ptr, Previous, q : PtEl;

    BEGIN
        ALLOCATE( q, SIZE( PtEl ) );
        q^.Name[0] := 'f';
        q^.Name[1] := 'F';
        q^.Rank := Rank;
        q^.Next := NIL;
        Ptr := First;
        WHILE (Ptr#NIL) AND (Ptr^.Rank<Rank) DO
            Previous := Ptr;
            Ptr := Ptr^.Next;
        END;
        IF First=NIL THEN
            First := q;
        ELSIF First=Ptr THEN
            First := q;
            q^.Next := Ptr;
        ELSIF Ptr=NIL THEN
            Previous^.Next := q;
        ELSE
            Previous^.Next := q;
            q^.Next := Ptr;
        END;
    END Insert;

    PROCEDURE Delete( Rank : CARDINAL );

        VAR
            Ptr, Previous : PtEl;

    BEGIN
        Ptr := First;
        IF Ptr#NIL THEN
            WHILE (Ptr#NIL) AND (Ptr^.Rank#Rank) DO
                Previous := Ptr;
                Ptr := Ptr^.Next;
            END;
            IF Ptr=First THEN
                First := Ptr^.Next;
                DEALLOCATE( Ptr, SIZE( PtEl ) );
            ELSIF Ptr=NIL THEN
                WriteString( "      Rank not found" );
            ELSE
                Previous^.Next := Ptr^.Next;
                DEALLOCATE( Ptr, SIZE( PtEl ) );
            END;
        END;
    END Delete;


    PROCEDURE WriteList;

        VAR
            Ptr : PtEl;

    BEGIN
        Ptr := First;
        WHILE Ptr#NIL DO
            Write( Ptr^.Name[0] );
            Write( Ptr^.Name[1] );
            Write( ' ' );
            WriteCard( Ptr^.Rank, 4 );
            WriteLn;
            Ptr := Ptr^.Next;
        END;
    END WriteList;


BEGIN
    First := NIL;
    FOR i := Start TO Finish BY 2 DO
        Insert( i );
    END;
    WriteList;
    FOR i := Start+1 TO Finish BY 2 DO
        Insert( i );
    END;
    WriteList;
    FOR i := Finish TO Start BY -3 DO
        Delete( i );
    END;
    WriteList;
    HoldScreen;
END test.

So nothing execpected happens when it is run. Now make 3 small changes, first
to the String TYPE...

    String : ARRAY [0..2] OF CHAR;

and add the following to Insert...

    q^.Name[2] := 'x';

and this to WriteList...

    Write( Ptr^.Name[2] );

It will compile Ok, but when it runs!!! The output characters, the Writes in
WriteList, print out junk and the CARDINAL Rank eventually ( even with in the
first three lines ) starts to contain incorrect values. 5423 for example.

Well what have I done wrong? I cant tell and I strongly suspect the compiler.
There are better PD compilers for the IBM than this if it is a bug. Also I was
thinking of upgrading to the next ( expensive ) version, the PSE. With this as
a track record I am mightily tempted to stick with C. Any comments on the PSE
version?

Peter Hi


        Peter "You're doing computing as an ARTS degree!" Hickman
      COGS U/G PH, University of Sussex, Falmer, Brighton, BN1 9QH
--------------------------------------- peterhi@uk.ac.sussex.syma ------------
    "Lego is the Mechano of the intellectual cripple" - Aaron Sloman
--------------------------------------- peterhi@uk.ac.sussex.tsuna -----------
      "More beer, more shouting, resistance is useless" - USTA bars

ldo@waikato.ac.nz (Lawrence D'Oliveiro, Waikato University) (01/31/91)

	 ALLOCATE( q, SIZE( PtEl ) );
			    ^^^^
Aha!

Try the following:

	 ALLOCATE(q, SIZE(Element));
			  ^^^^^^^

I made this change and got the program to work just fine, both in its
original form and with the changes you described.

*Without* this fix, the program completely hung up my Mac. On breaking
into MacsBug, it told me that the application heap had suffered major
damage. No wonder...

By the way, I use the MPW version of the Metrowerks compiler. I've only
been using it for about four months, but so far it looks like the most
bug-free of the various Mac compilers I've used.

Lawrence D'Oliveiro                       fone: +64-71-562-889
Computer Services Dept                     fax: +64-71-384-066
University of Waikato            electric mail: ldo@waikato.ac.nz
Hamilton, New Zealand    37^ 47' 26" S, 175^ 19' 7" E, GMT+13:00

BJ020000@NDSUVM1.BITNET (Dave Mueller) (01/31/91)

This is a spool of what happens when it is run under Waterloo Mod-2..
The reason you start getting junk output could be 'cause you have wandered
into unallocated territory..

Dave
Ready; T=0.01/0.01 09:45:35
mw tst mod
Waterloo Modula-2 (version 2)
File 'TST MOD A0': 109 lines, modules imported 2, no errors
fF    1
fF    3
fF    5
fF    7
fF    9
fF    1
fF    2
fF    3
fF    4
fF    5
fF    6
fF    7
fF    8
fF    9
fF   10
Bad free list.
Traceback started for machine address 02ee28
Base      Return    Activation
Register  Address   Record

02edda    02ee28    0ab7dc
An 'addressing' exception has occurred
Traceback started for machine address 02246a
Base      Return    Activation
Register  Address   Record

0a78a4    0a7998    0ab9e4
07e9c8    07ea26    0ab944
07e920    07e9a4    0ab90c
05fde4    05fe2a    0ab8e4
02343e    023466    0ab89c
023360    0233aa    0ab85c
028840    028872    0ab80c
02edda    02ee28    0ab7dc
05f01e    05f03a    0ab7b4
0d530c    0d556e    0ab784      TST.Delete at line 71
0d575a    0d58a8    0ab75c      TST.Main at line 106

Traceback ended
Ready; T=0.25/0.29 09:45:48
cons get

peterhi@syma.sussex.ac.uk (Peter Hickman) (02/01/91)

Thank you, and  all those  other people,  for pointing  out the  cause of  the
problem. I have been staring  vainly for many days at  the code. I think  that
the problem was that the first program actually worked ( honest it did ) and I
thus assumed it was the changes that  introduced the error not just make  them
manifest. Thanks again.

Peter 'Wally' Hi