Board index » delphi » stack overflow

stack overflow

hi....i'm writing a program that uses a lot of functions and procedures yet
when I try to run it, it give me error 202: stack overflow error
so i did a little experimenting...
I started with one basic procedure and this works
I then add another procedure (that calls on the previous one) and the
program still works
so i add another procedure (that calls on the previous one as well) but the
program gives me this error...
if anyone could help i'd appreciate it a lot

::jen::

btw, here's the program if anyone is curious, it does calculations for BIG
numbers, bigger than normal integer and longint can handle

procedure addition(num1,num2:string; var new:string);
var
   sum_ch:string;
   i,num1_int,num2_int,carry,sum:integer;
begin
     carry:=0;
     new:='';
     if length(num1)<>length(num2) then
        if length(num1)>length(num2) then
           for i:=1 to length(num1)-length(num2) do    {adds leading zeros}
                                                       {so numbers are same
length}
               num2:='0'+num2
        else
            for i:=1 to length(num2)-length(num1) do
                num1:='0'+num1;

     for i:=length(num1) downto 1 do
         begin
              num1_int:=ord(num1[i])-48;
              num2_int:=ord(num2[i])-48;

              sum:=num1_int+num2_int+carry;
              carry:=sum div 10;

              if i=1 then
                 str(sum,sum_ch)
              else
                  begin
                       sum:=sum mod 10;
                       sum_ch:=chr(48+sum);
                  end;
              new:=sum_ch+new;

         end;
end;

procedure multiplication(num1,num2:string; var new:string);
var
   small,big,sum_ch,temp:string;
   newn:array[1..50] of string;
   a,i,carry,sum,num1_int,num2_int:integer;
begin
     carry:=0;
     new:='';
     if length(num1)>=length(num2) then
        begin
             big:=num1;
             small:=num2;
        end
     else
         begin
              big:=num2;
              small:=num1;
         end;

     for i:=1 to length(small) do
         newn[i]:='';
     for i:=2 to length(small) do
         for a:=1 to i-1 do
             newn[i]:=newn[i]+'0';
     for i:=length(small) downto 1 do
         for a:=length(big) downto 1 do
             begin
                  num1_int:=ord(num1[a])-48;
                  num2_int:=ord(num2[i])-48;

                  sum:=num1_int*num2_int+carry;
                  carry:=sum div 10;

                  if a=1 then
                     str(sum,sum_ch)
                  else
                      begin
                           sum:=sum mod 10;
                           sum_ch:=chr(48+sum);
                      end;

                  newn[length(small)-i+1]:=sum_ch+newn[length(small)-i+1];

                  if a=1 then
                     carry:=0;   {reset carry when doing second line}
                                        {of multiplication}

             end;
     addition(newn[1],newn[2],temp);    {
     new:=temp;                          }
     if length(small)>2 then
        for a:=3 to length(small) do
            begin
                 addition(newn[a],temp,new);
                 temp:=new;
            end;
     new:=temp;
end;

procedure sqr_root(num1:string; var new:string);
var
   jen2,temp:string;
begin
    temp:=num1;
    multiplication(temp,temp,jen2);
    new:=jen2;
end;

var
   num1,num2,new:string;
begin
     write('Enter a number: ');
     readln(num1);
     sqr_root(num1,new);
     writeln(new);
     readln;
end.

 

Re:stack overflow


Quote
"::jen::" <jennifer-...@home.com> wrote in message

news:9Y4B7.93050$5h5.36464544@news3.rdc2.on.home.com...

Quote
> hi....i'm writing a program that uses a lot of functions and procedures
yet
> when I try to run it, it give me error 202: stack overflow error

Hi Jen,

Immediate solution: increase your stack size in the Compiler options.

(Increasing it from 16384 to 20000 enabled me to get your program to run.)

In the longer term, you may want to consider doing one or more of the
following:

* Using fixed-length strings instead of dynamic strings;

* Declaring some of your variables on the heap, in particular
your array of strings in the procedure 'multiplication';

* Converting to the PChar type; this gives you both longer
strings and heap storage, but is not recommended for the
inexperienced programmer.

--
The Scarlet Manuka

Other Threads