Hi, again
CBFalconer wrote:
Simplify your basic list handling and publish the whole thing. It is highly suspect, because you are in the habit of making copies of pointers in a rather undisciplined manner. You are not working with the lists you think you are.
I have to admit that everything I (formally) learned about programming I learned in Fortran on punch cards. But I didn't think my pointer work was all that bad!
Below is the program, stripped of everything but the list handling. Now it works perfectly, of course. But perhaps the error is still lurking? I'd appreciate any suggestions. I'm flummoxed.
The basic idea is that I start with a list with a bunch of entries. I pass through the list, using information in it to generate a new list. Each record I examine, I dispose. I then pass the new list to the old one (pointer), and repeat. Eventually, there are no more entries, and the program moves on to the next realization (main loop).
I thought perhaps there was a systematic memory leak, so I have two simple detection schemes. One is that I print out the starting address of the list for each realization (no trend, just random drift). The other is that each pass through the first list should generate the same number of records that is deleted on the next pass (this is the countIn, countOut, prevIn stuff - it also checks out).
regards, Toby ------------------------ Program Snowbird;
Const Realize = 20; initial = 6500;
Type PRec = ^Rec; Rec = record rx, ry, rz : shortCard; check : PtrCard; next : PRec; end;
var r : shortCard; PrevIn : medCard; {________________________________________________ }
Procedure NumLatt(var PrevIn : medCard); var Root : PRec; countIn : medCard; i : shortCard; {-----------------------} Procedure LInsert(var Root : PRec; x, y, z : shortCard); var temp : PRec; begin New(temp); with temp^ do begin rx := x; ry := y; rz := z; check := PtrCard(temp); next := Root; end; Root := temp; end; {LInsert} {-----------------------} Procedure Advance(var Root : PRec; var countIn, PrevIn : medCard); var LNext, PSelf : PRec; i, countOut : medCard;
begin LNext := Nil; countOut := 0; while (Root <> Nil) do begin {walk through Root list} for i := 1 to trunc(2.98*random) do begin {build LNext list} inc(countIn); LInsert(LNext, i, 2*i, 3*i); end; PSelf := Root; {destroy Root in passing} Root := Root^.next; inc(countOut); Dispose(PSelf); PSelf := Nil; end; Root := LNext; {give LNext to Root} LNext := Nil; if (countOut <> PrevIn) then begin {do numbers balance?} writeln('error: countOut = ', countOut:1, ', PrevIn = ', PrevIn:1); readln; end; PrevIn := countIn; end; {Advance} {-----------------------}
begin {NumLatt} Root := Nil; LInsert(Root, 1, 2, 3); writeln('Root starts at ', Root^.check:1); for i := 2 to Initial do LInsert(Root, i, 2*i, 3*i); repeat countIn := 0; Advance(Root, countIn, PrevIn); until (countIn = 0); end; {________________________________________________ }
begin {Main Program} writeln; writeln('List checking'); writeln; Randomize; for r := 1 to Realize do begin write('Realization ', r:2, ': '); PrevIn := Initial; NumLatt(PrevIn); end; end.