#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# help me!!!

Posts: 20Member
[b][red]This message was edited by chrizlee at 2003-4-9 13:7:11[/red][/b][hr]
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

[code]
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;

begin
clrscr;
writeln('Please enter the number you wish to search in the 5000 list and 1000 list:');
end;

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

begin
intent;
main(num);
end.
[/code]

· ·

• Try:

[code]
procedure linear([b]var[/b] no: numbers; numsearch: integer);
[/code]

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
:
: [code]
: 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<>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;
:
: begin
: clrscr;
: writeln('Please enter the number you wish to search in the 5000 list and 1000 list:');
: end;
:
: procedure main(var no: numbers);
: var numsearch: integer;
: begin
: repeat
: assign(no);
: output1;
: (*****)
: linear(no,numsearch);
: writeln('Press c to repeat the program.');
: writeln('Press q to quit.');
: donewincrt;
: end;
:
: begin
: intent;
: main(num);
: end.
: [/code]
:

· ·
• Posts: 20Member
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!

: [b][red]This message was edited by chrizlee at 2003-4-9 13:7:11[/red][/b][hr]
: 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
:
: [code]
: 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;
:
: begin
: clrscr;
: writeln('Please enter the number you wish to search in the 5000 list and 1000 list:');
: end;
:
: procedure main(var no: numbers);
: var numsearch: integer;
: begin
: repeat
: assign(no);
: output1;
: (*****)
: linear(no,numsearch);
: writeln('Press c to repeat the program.');
: writeln('Press q to quit.');
: donewincrt;
: end;
:
: begin
: intent;
: main(num);
: end.
: [/code]
:
:
:

· ·
• Posts: 1,621Member
: 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?

: [code]
: 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;
: [/code]

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.
· ·
• Posts: 757Member
[b][red]This message was edited by Phat Nat at 2003-4-10 14:21:33[/red][/b][hr]
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
:
[code]
type
numbers=array[1..5000] of integer;

var
num: numbers;

procedure assign(var no: numbers);
var x: integer;
begin
{...}
end.
[/code]

· ·
• 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: [b]using "var" prevents from copying the whole array onto the stack when calling "linear"[/b]. 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!
· ·
• Posts: 20Member
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?
:
: : [code]
: : 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;
: : [/code]
:
: 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.
:

· ·
• Posts: 20Member
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: [b]using "var" prevents from copying the whole array onto the stack when calling "linear"[/b]. 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!
:

· ·
• Posts: 1,621Member
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: [b]using "var" prevents from copying the whole array onto the stack when calling "linear"[/b]. 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!
: :
:
:

· ·
• Posts: 20Member
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: [b]using "var" prevents from copying the whole array onto the stack when calling "linear"[/b]. 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!
: : :
: :
: :
:
:

· ·
• Posts: 1,621Member
: 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().
· ·