problem with stack overflow error - Programmers Heaven

#### Howdy, Stranger!

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

# problem with stack overflow error

Posts: 20Member
[b][red]This message was edited by chrizlee at 2003-4-30 5:18:7[/red][/b][hr]
[b][red]This message was edited by chrizlee at 2003-4-29 17:23:30[/red][/b][hr]
This is a recursive procedure in my 8 queens program. but i keep having error 202 stack overflow. i really don't know how to fix it coz i've tried a lot of different ways. is there a better way to do this? thz

kel

[code]
procedure pos_search(var queen: array[1..8] of integer;
var num: integer;
var col: integer;
var row: integer);
var x,y: integer;
done: boolean;
begin

If row<9 then
done:=true
else done:=false;

for x:= 1 to col-1 do
begin

If (row-col=queen[x]-x) or (row+col=queen[x]+x) or (row=queen[x]) then
done:=false;

end;

If done=true then
begin
queen[col]:=row;
If col>7 then
begin
inc(num);
display(queen,num);
inc(row);
end
else begin
row:=1;
inc(col);
end;

end
else If row>7 then
begin
queen[col]:=0;
dec(col);
writeln('row ',row,' -1col ',col-1);
row:=queen[col]+1;
end
else begin
inc(row);
end;

If queen[1]<>0 then
pos_search(queen,num,col,row);

end;
[/code]

## Comments

• Posts: 757Member
What is your POSITION type? How large is it? Since this is a recursive function, any variables that you declare within it are allocated in memory each time it is called. If you can cut down on the number of variables that you are passing and variables that you are declaring in the procedure's VAR area. This is the best thing to do.

Also, I just noticed this code that could send it into an infinite loop:
[code]
If queen[[red]1[/red]]<>0 then
pos_search(queen,num,col,row);
[/code]
This is always checking the first queen, so if it's <>0 then it will go into an infinite loop.

Phat Nat

: [b][red]This message was edited by chrizlee at 2003-4-29 17:23:30[/red][/b][hr]
: This is a recursive procedure in my 8 queens program. but i keep having error 202 stack overflow. i really don't know how to fix it coz i've tried a lot of different ways. is there a better way to do this? thz
:
: kel
:
: [code]
: procedure pos_search(var queen: position;
: var num: integer;
: var col: integer;
: var row: integer);
: var x,y: integer;
: done: boolean;
: begin
:
: If row<9 then
: done:=true
: else done:=false;
:
: for x:= 1 to col-1 do
: begin
:
: If (row-col=queen[x]-x) or (row+col=queen[x]+x) or (row=queen[x]) then
: done:=false;
:
: end;
:
: If done=true then
: begin
: queen[col]:=row;
: If col>7 then
: begin
: inc(num);
: display(queen,num);
: inc(row);
: end
: else begin
: row:=1;
: inc(col);
: end;
:
: end
: else If row>7 then
: begin
: queen[col]:=0;
: dec(col);
: writeln('row ',row,' -1col ',col-1);
: row:=queen[col]+1;
: end
: else begin
: inc(row);
: end;
:
: If queen[1]<>0 then
: pos_search(queen,num,col,row);
:
: end;
: [/code]
:
:
:
:

• Posts: 20Member
the queen array is the rows that the queens are in.
[code] If queen[1]<>0 then
pos_search(queen,num,col,row)
[/code]
this checks if all the solutions are found. if the first queen is set to zero, which means it's terminated, then it doesn't repeat the pos_search procedure again.
i've checked the procedure step by step with all the information displayed on the screen every time. the procedure was on the right track but stack overflow before it can reach the first solution.

kel

: What is your POSITION type? How large is it? Since this is a recursive function, any variables that you declare within it are allocated in memory each time it is called. If you can cut down on the number of variables that you are passing and variables that you are declaring in the procedure's VAR area. This is the best thing to do.
:
: Also, I just noticed this code that could send it into an infinite loop:
: [code]
: If queen[[red]1[/red]]<>0 then
: pos_search(queen,num,col,row);
: [/code]
: This is always checking the first queen, so if it's <>0 then it will go into an infinite loop.
:
: Phat Nat
:
:
: : [b][red]This message was edited by chrizlee at 2003-4-29 17:23:30[/red][/b][hr]
: : This is a recursive procedure in my 8 queens program. but i keep having error 202 stack overflow. i really don't know how to fix it coz i've tried a lot of different ways. is there a better way to do this? thz
: :
: : kel
: :
: : [code]
: : procedure pos_search(var queen: position;
: : var num: integer;
: : var col: integer;
: : var row: integer);
: : var x,y: integer;
: : done: boolean;
: : begin
: :
: : If row<9 then
: : done:=true
: : else done:=false;
: :
: : for x:= 1 to col-1 do
: : begin
: :
: : If (row-col=queen[x]-x) or (row+col=queen[x]+x) or (row=queen[x]) then
: : done:=false;
: :
: : end;
: :
: : If done=true then
: : begin
: : queen[col]:=row;
: : If col>7 then
: : begin
: : inc(num);
: : display(queen,num);
: : inc(row);
: : end
: : else begin
: : row:=1;
: : inc(col);
: : end;
: :
: : end
: : else If row>7 then
: : begin
: : queen[col]:=0;
: : dec(col);
: : writeln('row ',row,' -1col ',col-1);
: : row:=queen[col]+1;
: : end
: : else begin
: : inc(row);
: : end;
: :
: : If queen[1]<>0 then
: : pos_search(queen,num,col,row);
: :
: : end;
: : [/code]
: :
: :
: :
: :
:
:

Sign In or Register to comment.