Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
help me!!! Posted by chrizlee on 9 Apr 2003 at 5:49 AM
This message was edited by chrizlee at 2003-4-9 13:7:11

i run this in pascal and it gives me error 202: stack overflow at (****),right before the linear procedure. i tried to figure it out for so long, but i just dunno why. can someone help me?
thz

program search;

uses
    wincrt,kelly,winprocs;

type
    numbers=array[1..5000] of integer;

var
   num: numbers;

   procedure assign(var no: numbers);
   var x: integer;
             begin
                  for x:= 1 to 5000 do
                      no[x]:=x;       
             end;                 

   procedure output2(ticknum: longint);
             begin
                  write('  ',ticknum);
             end;

   procedure linear(no: numbers; numsearch: integer);
   var x,y: integer;
       length: integer;
       ticknum: longint;
       done,start: longint;
       begin
            for y:= 1 to 2 do
                begin

                     If y=1 then
                        length:=1000
                     else length:=5000;

                     x:=0;
                     start:=gettickcount;

                     while (numsearch<>no[x]) and (x<=length) do
                           inc(x);
                     done:=gettickcount;
                     ticknum:=done-start;
                     write(' Linear');
                     output2(ticknum);
                end;
            writeln;
       end;

   procedure output1;
             begin
                  clrscr;
                  writeln('   Search    1000 Items     5000 Items');
             end;

   procedure ask(var numsearch: integer);
             begin
                  clrscr;
                  writeln('Please enter the number you wish to search in the 5000 list and 1000 list:');
                  readln(numsearch);
             end;

   procedure main(var no: numbers);
   var numsearch: integer;
          begin
               repeat
                     ask(numsearch);
                     assign(no);              
                     output1;
                     (*****)
                     linear(no,numsearch);
                     writeln('Press c to repeat the program.');
                     writeln('Press q to quit.');
               until readkey='q';
               donewincrt;
          end;

begin
     intent;
     main(num);
end.



Report
Re: help me!!! Posted by tron on 9 Apr 2003 at 7:42 AM
Try:

  procedure linear(var no: numbers; numsearch: integer);


Besides: You aren't using the "no" array in your "linear" procedure!? Therefore you can leave it out - or maybe it's an error? ...

tron.

: i run this in pascal and it gives me error 202: stack overflow at (****),right before the linear procedure. i tried to figure it out for so long, but i just dunno why. can someone help me?
: thz
:
:
: program search;
: 
: uses
:     wincrt,kelly,winprocs;
: 
: type
:     numbers=array[1..5000] of integer;
: 
: var
:    num: numbers;
: 
:    procedure assign(var no: numbers);
:    var x: integer;
:              begin
:                   for x:= 1 to 5000 do
:                       no[x]:=x;       
:              end;                 
: 
:    procedure output2(ticknum: longint);
:              begin
:                   write('  ',ticknum);
:              end;
: 
:    procedure linear(no: numbers; numsearch: integer);
:    var x,y: integer;
:        length: integer;
:        ticknum: longint;
:        done,start: longint;
:        begin
:        readkey;
:             for y:= 1 to 2 do
:                 begin
: 
:                      If y=1 then
:                         length:=1000
:                      else length:=5000;
: 
:                      x:=0;
:                      start:=gettickcount;
: 
:                      while (numsearch<>x) and (x<=length) do
:                            inc(x);
:                      done:=gettickcount;
:                      ticknum:=done-start;
:                      write(' Linear');
:                      output2(ticknum);
:                 end;
:             writeln;
:        end;
: 
:    procedure output1;
:              begin
:                   clrscr;
:                   writeln('   Search    1000 Items     5000 Items');
:              end;
: 
:    procedure ask(var numsearch: integer);
:              begin
:                   clrscr;
:                   writeln('Please enter the number you wish to search in the 5000 list and 1000 list:');
:                   readln(numsearch);
:              end;
: 
:    procedure main(var no: numbers);
:    var numsearch: integer;
:           begin
:                repeat
:                      ask(numsearch);
:                      assign(no);              
:                      output1;
:                      (*****)
:                      linear(no,numsearch);
:                      writeln('Press c to repeat the program.');
:                      writeln('Press q to quit.');
:                until readkey='q';
:                donewincrt;
:           end;
: 
: begin
:      intent;
:      main(num);
: end.
: 

:

Report
Re: help me!!! Posted by chrizlee on 10 Apr 2003 at 5:51 AM
but i'm trying to find a specific number in the array though, i don't need to change the array, that's y i didn't use "var". i tried bracketing half of the procedure out to make sure memory wouldn't be a problem, but it didn't work. i even tried changing the "no" to "num" and procedure name. i really don't know what's wrong!

: This message was edited by chrizlee at 2003-4-9 13:7:11

: i run this in pascal and it gives me error 202: stack overflow at (****),right before the linear procedure. i tried to figure it out for so long, but i just dunno why. can someone help me?
: thz
:
:
: program search;
: 
: uses
:     wincrt,kelly,winprocs;
: 
: type
:     numbers=array[1..5000] of integer;
: 
: var
:    num: numbers;
: 
:    procedure assign(var no: numbers);
:    var x: integer;
:              begin
:                   for x:= 1 to 5000 do
:                       no[x]:=x;       
:              end;                 
: 
:    procedure output2(ticknum: longint);
:              begin
:                   write('  ',ticknum);
:              end;
: 
:    procedure linear(no: numbers; numsearch: integer);
:    var x,y: integer;
:        length: integer;
:        ticknum: longint;
:        done,start: longint;
:        begin
:             for y:= 1 to 2 do
:                 begin
: 
:                      If y=1 then
:                         length:=1000
:                      else length:=5000;
: 
:                      x:=0;
:                      start:=gettickcount;
: 
:                      while (numsearch<>no[x]) and (x<=length) do
:                            inc(x);
:                      done:=gettickcount;
:                      ticknum:=done-start;
:                      write(' Linear');
:                      output2(ticknum);
:                 end;
:             writeln;
:        end;
: 
:    procedure output1;
:              begin
:                   clrscr;
:                   writeln('   Search    1000 Items     5000 Items');
:              end;
: 
:    procedure ask(var numsearch: integer);
:              begin
:                   clrscr;
:                   writeln('Please enter the number you wish to search in the 5000 list and 1000 list:');
:                   readln(numsearch);
:              end;
: 
:    procedure main(var no: numbers);
:    var numsearch: integer;
:           begin
:                repeat
:                      ask(numsearch);
:                      assign(no);              
:                      output1;
:                      (*****)
:                      linear(no,numsearch);
:                      writeln('Press c to repeat the program.');
:                      writeln('Press q to quit.');
:                until readkey='q';
:                donewincrt;
:           end;
: 
: begin
:      intent;
:      main(num);
: end.
: 

:
:
:

Report
Re: help me!!! Posted by tron on 10 Apr 2003 at 8:03 AM
I see you have changed your code, so that your "linear" procedure is using "no" now. Well, "somehow" I knew that something was wrong with ...

But you still can get a StackOverflow because you are not using the "var" indicator. The "var" doesn't simply mean that changes to the array will be visible outside the procedure - in this case, what is much more important: using "var" prevents from copying the whole array onto the stack when calling "linear". Your array has 5000 entries and an integer is (usualy, at least in TP) 2Bytes - so that the array has 10,000 Bytes. This might be too much for your stack configuration.

Whenever you pass a big array into a procedure/function, you should think about using the "var" indicator.

tron.

: but i'm trying to find a specific number in the array though, i don't need to change the array, that's y i didn't use "var". i tried bracketing half of the procedure out to make sure memory wouldn't be a problem, but it didn't work. i even tried changing the "no" to "num" and procedure name. i really don't know what's wrong!
Report
Re: help me!!! Posted by chrizlee on 10 Apr 2003 at 5:48 PM
o, maybe that's the problem. i'll have to try that. thz

: I see you have changed your code, so that your "linear" procedure is using "no" now. Well, "somehow" I knew that something was wrong with ...
:
: But you still can get a StackOverflow because you are not using the "var" indicator. The "var" doesn't simply mean that changes to the array will be visible outside the procedure - in this case, what is much more important: using "var" prevents from copying the whole array onto the stack when calling "linear". Your array has 5000 entries and an integer is (usualy, at least in TP) 2Bytes - so that the array has 10,000 Bytes. This might be too much for your stack configuration.
:
: Whenever you pass a big array into a procedure/function, you should think about using the "var" indicator.
:
: tron.
:
: : but i'm trying to find a specific number in the array though, i don't need to change the array, that's y i didn't use "var". i tried bracketing half of the procedure out to make sure memory wouldn't be a problem, but it didn't work. i even tried changing the "no" to "num" and procedure name. i really don't know what's wrong!
:

Report
Re: help me!!! Posted by Manning on 10 Apr 2003 at 6:15 PM
It definitely is the problem. I just downloaded your code to test it, and that's when I noticed that you are compiling a windows target, which (in BP at least) has a default stack size of 8k.

As was pointed out, your array is 10k, and if you dont use var, then it has to create the local variable instead of use the global variable, and of course putting a 10k variable on an 8k stack just isnt going to work.

So either tweak the stack size, or use var. Either way will work.


: o, maybe that's the problem. i'll have to try that. thz
:
: : I see you have changed your code, so that your "linear" procedure is using "no" now. Well, "somehow" I knew that something was wrong with ...
: :
: : But you still can get a StackOverflow because you are not using the "var" indicator. The "var" doesn't simply mean that changes to the array will be visible outside the procedure - in this case, what is much more important: using "var" prevents from copying the whole array onto the stack when calling "linear". Your array has 5000 entries and an integer is (usualy, at least in TP) 2Bytes - so that the array has 10,000 Bytes. This might be too much for your stack configuration.
: :
: : Whenever you pass a big array into a procedure/function, you should think about using the "var" indicator.
: :
: : tron.
: :
: : : but i'm trying to find a specific number in the array though, i don't need to change the array, that's y i didn't use "var". i tried bracketing half of the procedure out to make sure memory wouldn't be a problem, but it didn't work. i even tried changing the "no" to "num" and procedure name. i really don't know what's wrong!
: :
:
:

Report
Re: help me!!! Posted by chrizlee on 10 Apr 2003 at 7:43 PM
yes, tron and manning, you guys are right. i just added var and it RUNS! but is there a way other than using var?

: It definitely is the problem. I just downloaded your code to test it, and that's when I noticed that you are compiling a windows target, which (in BP at least) has a default stack size of 8k.
:
: As was pointed out, your array is 10k, and if you dont use var, then it has to create the local variable instead of use the global variable, and of course putting a 10k variable on an 8k stack just isnt going to work.
:
: So either tweak the stack size, or use var. Either way will work.
:
:
: : o, maybe that's the problem. i'll have to try that. thz
: :
: : : I see you have changed your code, so that your "linear" procedure is using "no" now. Well, "somehow" I knew that something was wrong with ...
: : :
: : : But you still can get a StackOverflow because you are not using the "var" indicator. The "var" doesn't simply mean that changes to the array will be visible outside the procedure - in this case, what is much more important: using "var" prevents from copying the whole array onto the stack when calling "linear". Your array has 5000 entries and an integer is (usualy, at least in TP) 2Bytes - so that the array has 10,000 Bytes. This might be too much for your stack configuration.
: : :
: : : Whenever you pass a big array into a procedure/function, you should think about using the "var" indicator.
: : :
: : : tron.
: : :
: : : : but i'm trying to find a specific number in the array though, i don't need to change the array, that's y i didn't use "var". i tried bracketing half of the procedure out to make sure memory wouldn't be a problem, but it didn't work. i even tried changing the "no" to "num" and procedure name. i really don't know what's wrong!
: : :
: :
: :
:
:

Report
Re: help me!!! Posted by Manning on 11 Apr 2003 at 5:35 AM
: yes, tron and manning, you guys are right. i just added var and it RUNS! but is there a way other than using var?

Yes there are a couple alternatives. The 2 easiest would be:

1) Increase the stack size. Something like 16384 should give you enough space for the 10k array as well as any other local variables you use.

2) Make the array a global variable that all your functions can read, and abandon the parameter passing method. Actually, it already is a global variable because of where you defined it, you just arent using it as such.

Your teacher probably wont like it if you do #2 though. In the beginning they dont want to confuse students too much, so they usually say "global variables are bad, dont use them". For the most part that is true, but there are times when they can be useful. I dont know if this would be one of those times anyway, since there are better alternatives.

And if you use #1 and this is an assignment you have to hand in for your teacher to run, you'll have to make sure you use the compiler directive ($M I believe) to change the stack size. If you change it in the compiler options, then the teacher will also have to make the same change in their IDE, which they wont want to do for each student (and if you forget to tell them, they'll get the run-time error and give you a 0 or something...)

Personally, I would stick to using var. It's more efficient both memory and speed wise, since you dont have to replicate the data each time you call linear().
Report
Re: help me!!! Posted by Manning on 10 Apr 2003 at 6:04 AM
: i run this in pascal and it gives me error 202: stack overflow at (****),right before the linear procedure. i tried to figure it out for so long, but i just dunno why. can someone help me?

:
: type
:     numbers=array[1..5000] of integer;
:
:    procedure linear(no: numbers; numsearch: integer);
:        begin
:                      x:=0;

:                      while (numsearch<>no[x]) and (x<=length) do
:                            inc(x);
:        end;
: 


I chopped your code down to what I see as the relevant bits. The numbers type you define is an array that goes from 1 to 5000. In your linear procedure you pass in a numbers type called no. And in that procedure you loop through the array trying to find the matching number.

What I see as a problem is that you initialize x to 0, so the first time through that while loop you are evaluating no[0], which is invalid since your array goes from 1 to 5000.

I would think this would raise a different error than stack overflow (unless maybe you disabled range checking?), so it may not be the cause of the problem you posted your message about, but it is a problem nonetheless.
Report
Re: help me!!! Posted by chrizlee on 10 Apr 2003 at 5:46 PM
o ya, that's another error, but i haven't had a chance to find that one since i'm busy with the first error. thz

: : i run this in pascal and it gives me error 202: stack overflow at (****),right before the linear procedure. i tried to figure it out for so long, but i just dunno why. can someone help me?
:
: :
: : type
: :     numbers=array[1..5000] of integer;
: :
: :    procedure linear(no: numbers; numsearch: integer);
: :        begin
: :                      x:=0;
: 
: :                      while (numsearch<>no[x]) and (x<=length) do
: :                            inc(x);
: :        end;
: : 

:
: I chopped your code down to what I see as the relevant bits. The numbers type you define is an array that goes from 1 to 5000. In your linear procedure you pass in a numbers type called no. And in that procedure you loop through the array trying to find the matching number.
:
: What I see as a problem is that you initialize x to 0, so the first time through that while loop you are evaluating no[0], which is invalid since your array goes from 1 to 5000.
:
: I would think this would raise a different error than stack overflow (unless maybe you disabled range checking?), so it may not be the cause of the problem you posted your message about, but it is a problem nonetheless.
:

Report
Re: help me!!! Posted by Phat Nat on 10 Apr 2003 at 7:59 AM
This message was edited by Phat Nat at 2003-4-10 14:21:33

The variable is 1..5000 and not 1..50000, right? If you try to initialize over 64,000 you will get that error and 2*50000 > 64000. Just a thought.

Phat Nat

BTW - Might be something with the KELLY Unit. If it is using a huge amount of memory that may be causing it. Pascal allows each unit to use 64k, but there is a maximum total as well I believe, as well as a maximum code length.

: i run this in pascal and it gives me error 202: stack overflow at (****),right before the linear procedure. i tried to figure it out for so long, but i just dunno why. can someone help me?
: thz
:
 type
     numbers=array[1..5000] of integer;
 
 var
    num: numbers;
 
    procedure assign(var no: numbers);
    var x: integer;
              begin
{...}
 end.







 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.