#GURU.error
6 messages in this thread
To Mike, Don, or anyone else who can find the mystery of the GURU 00000003.
address error I keep getting in my program. Here's an abridged version of
my program. All parts left out have nothing to do with the array a. static
int a[31][62] = { /* global array */
91,93,91,93,91,93,91,93,91,93,91,93,91,93,91,93,91,93,91,93,etc., I've
initialized exactly 1,922 elements filling all of array a.
}; main () {
title (); } direction (rw,cl,warn) /* FUNCTION TAKES DIRECTION FROM
KEYBOARD */
int rw, cl,warn; {
char inp[80];
int sig = 0;
if (warn == 1) { maze(rw,cl,warn); }
printf (">");
scanf ("%s",&inp);
switch (inp[0])
{
case 'n':
rw = rw – 1;
if (inp[1] == 'e') { cl = cl + 2; }
if (inp[1] == 'w') { cl = cl – 2; }
break;
case 's':
rw = rw + 1;
if (inp[1] == 'e') { cl = cl + 2; }
if (inp[1] == 'w') { cl = cl – 2; }
break;
case 'e':
cl = cl + 2;
break;
case 'w':
cl = cl – 2;
break;
default:
wrong(rw,cl,sig);
break;
}
maze (rw,cl); } maze (rw,cl,) /* DRAW MAZE FUNCTION */
int rw, cl,; {
int r, c;
if (rw < 0) {rw = 0;}
if (cl < 0) {cl = 0;}
if (rw > 24) {rw = 24;}
if (cl > 48) {cl = 48;}
for ( r = rw; r <= rw +6; ++r)
{
for ( c = cl; c <= cl +13; ++c)
{
printf ("%c",a[r][c]);
}
printf ("\n");
}
direction (rw,cl); } title () /* LEADING FUNCTION */ {
int ans = 0, sig = 1, warn = 1;
int rw = 12, cl = 24;
printf ("\n\n\n TITLE \n\n");
printf (" 1) A choice\n");
printf (" 2) Start game\n");
printf (">");
scanf ("%d", &ans);
if (ans == 1)
other function ();
else if (ans == 2) {
direction (rw,cl,warn);}
else
wrong (rw,cl,sig); } wrong (rw,cl,sig) I hope this is enough for you
to get an idea whats wrong. Thanks Henry
You've got a case of runaway recursion going on. direction() calls maze(),
but maze() also calls direction(). Those two will just keep ping-ponging
back and forth until you run out of stack space, and then 'poof' you'll
start overwriting stuff.
-Mike
Hi Mike. I hope I'm not becoming a pain. It's just that I'm teaching myself
C and although I think I'm coming along ok I'm probably asking some rather
boring questions to real programmers.
If this runaway recursion is causing my address errors, how can I get
away from it? You see I also tried passing array 'a' between functions (not
as a global) which I believe is allowed, and I had the same problem.
Thanks Mike. ,Henry
OK the problem is you have something like this:
a ()
{
b();
}
b ()
{
a();
}
Now let's say you call function 'a' here from main(). It will in turn call
function 'b'. Which will call function 'a', which will call function 'b',
etc. etc. without end. Each call takes up a certain amount of stack space,
and eventually you hit the end of the stack, and keep on going.
From looking at your program, I _think_ you need to remove the call to
direction() out of maze() and you should be OK.
-Mike
Henry –
I agree with the other replies, inasmuch as removing the recursive call
will stop the recursion from happening. There are instances, though, where
recursion is the "best" way to get something done. What you're probably
missing is a termination condition – i.e., "I've looked in every direction
already and there's no use looking any more, so return without making the
recursive call". It's still quite possible to overrun your stack this way
(if, for instance, to cover all possibilities you must make more recursive
calls than your stack will allow), so if you set a termination condition in
the recursion and find yourself still running out of stack, you'll probably
want to re-think your algorithm to eliminate most or all recursion. Hope
this helps …
Harry
Henry,
Mike's right…you're recursing…that is, as Mike's example shows
you…each function calls the other over and over again.
Rethink your logic so that you eliminate that and you should be ok.
Don