Board index » delphi » simple list doesnt work - pascal newbie need help

simple list doesnt work - pascal newbie need help

Hi.
I tried to code an vokabulary-program, which simply doesnt work, cause there
always is an run-202 error (stack overflow)...... so i traced the problem to
the procedure addlist..... here's an example:

type inhalt = record
       englisch, deutsch : string;
     end;
     zeiger = ^vokabel;
     vokabel = record
       i : inhalt;
       next : zeiger;
     end;

var root : zeiger;
    listlength : word;
    c : char;
    input : string;
    mode : boolean;

function empty(z : zeiger) : boolean;
begin
  empty := (z = nil);
end;
procedure addlist(var z : zeiger; eng, deu : string);
var neu : zeiger;
begin
  if not empty(z) then addlist(z^.next, eng, deu)
  else begin
    new(neu);
    neu^.i.englisch := eng;
    neu^.i.deutsch := deu;
    neu^.next := nil;
    z := neu;
    inc(listlength);
  end;
end;

begin
readln;
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
writeln('15');
addlist(root, 'blah', 'blah');
writeln('16');
addlist(root, 'blah', 'blah');
writeln('17');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
writeln('20:');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
writeln('25');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
writeln('30');
addlist(root, 'blah', 'blah');
addlist(root, 'blah', 'blah');
writeln('32');
addlist(root, 'blah', 'blah');
writeln('fertig');
readln;
end.

Sory for the big post.....
The procedure seems to crash, when there are more than 30 elements in the
list....
why?
Any help wpould be greatly appreciated.....
-Thx in advance.....
(pls dont reply via mail..... i dont check it very often.. so pls reply in
the group.... hx)

-Cornelius

 

Re:simple list doesnt work - pascal newbie need help


In article <3adf09f...@news.isis.de>,
Cornelius J. Hollingsworth <hollingswo...@gmx.net> wrote:

Quote
>Hi.
>I tried to code an vokabulary-program, which simply doesnt work, cause there
>always is an run-202 error (stack overflow)...... so i traced the problem to
>the procedure addlist..... here's an example:

>type inhalt = record
>function empty(z : zeiger) : boolean;
>begin
>  empty := (z = nil);
>end;
>procedure addlist(var z : zeiger; eng, deu : string);
>var neu : zeiger;
>begin
>  if not empty(z) then addlist(z^.next, eng, deu)

That is a no no. It causes huge recursion.  Each step takes large chunk
of the stack. In general you should not traverse the list for each
insertion. Instead insert the record at the beginning of the list. This
reverses the list though. If you do not want it reversed, keep track of
the end of the list and use that pointer to add directly to the end. The
problem with such traversing the list is that it works OK with small
samples but if one then starts to add thousands of elements things slow
down.

Recursion is rarely justified. Trees are for example structures that can
be handled nicely with recursion. Same does not apply to lists.

If you really want to use recursion then make the end and deu variable
or constant parameters. This, however, is like taking aspirin on a
broken leg.

Osmo

Re:simple list doesnt work - pascal newbie need help


"Cornelius J. Hollingsworth" schreef:

Quote

> Hi.
> I tried to code an vokabulary-program, which simply doesnt work, cause there
> always is an run-202 error (stack overflow)...... so i traced the problem to
> the procedure addlist..... here's an example:

> type inhalt = record
>        englisch, deutsch : string;
>      end;
>      zeiger = ^vokabel;
>      vokabel = record
>        i : inhalt;
>        next : zeiger;
>      end;

> var root : zeiger;
>     listlength : word;
>     c : char;
>     input : string;
>     mode : boolean;

> function empty(z : zeiger) : boolean;
> begin
>   empty := (z = nil);
> end;
> procedure addlist(var z : zeiger; eng, deu : string);
> var neu : zeiger;
> begin
>   if not empty(z) then addlist(z^.next, eng, deu)
>   else begin
>     new(neu);
>     neu^.i.englisch := eng;
>     neu^.i.deutsch := deu;
>     neu^.next := nil;
>     z := neu;
>     inc(listlength);
>   end;
> end;

> begin
> readln;
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> writeln('15');
> addlist(root, 'blah', 'blah');
> writeln('16');
> addlist(root, 'blah', 'blah');
> writeln('17');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> writeln('20:');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> writeln('25');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> writeln('30');
> addlist(root, 'blah', 'blah');
> addlist(root, 'blah', 'blah');
> writeln('32');
> addlist(root, 'blah', 'blah');
> writeln('fertig');
> readln;
> end.

> Sory for the big post.....
> The procedure seems to crash, when there are more than 30 elements in the
> list....
> why?
> Any help wpould be greatly appreciated.....
> -Thx in advance.....
> (pls dont reply via mail..... i dont check it very often.. so pls reply in
> the group.... hx)

> -Cornelius

I think you forgot 'root:=nil;' at the beginning
of main. addlist is called recursively when root<>nil.

Maybe it calls itself about 2000 times in each addition,
before it finds some zero/nil somewhere in memory.
Each procedure-call is placed on the stack and now there
are simply to many calls to fit into the stack-space.

Found out by using debug with display stack or something
(I forgot)

Greetings. Huub.

Other Threads