🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #1716

Problem

#1716
From: Peter Hickman
Date: Wed, 30 Jan 1991 16:57
148 lines
4320 bytes
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
Technical Details
Message-ID: <4376@syma.sussex.ac.uk>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!icdoc!syma!peterhi

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!icdoc!syma!peterhi
From: peterhi@syma.sussex.ac.uk (Peter Hickman)
Newsgroups: comp.lang.modula2
Subject: Problem
Message-ID: <4376@syma.sussex.ac.uk>
Date: 30 Jan 91 16:57:57 GMT
Organization: University of Sussex
Lines: 148