Jump to content


weird crash bug


  • You cannot reply to this topic
4 replies to this topic

#1 hunguptodry

    New Member

  • Members
  • PipPip
  • 31 posts

Posted 28 July 2009 - 11:27 AM

following are 2 code fragments of the same function.
the only difference is the placement of the first line.
the first one crashes, the second does not.
does not crash on every call but is
consistent and repeatable.

I am using bloodshed Dev C++ 4.9.9.2

int posKong(int n, int tk) { int dp[144],tl[4],cnt = 0;

    int gp = _tiles[n].gp; int nm = _tiles[n].nm;

    int i,j,k; j = 0; for (i=0; i<144; i++) {

        if (_tiles[i].tk == tk && i != n) {

            if (_tiles[i].gp == gp &&

                _tiles[i].nm == nm) {

                cnt++;

            }

            dp[j++] = i;

        }

    }

    Bubble(tk,dp,j);

    if (cnt == 3) {

        k = 0; for (i=0; i<j; i++) {

            if (_tiles[dp[i]].gp == gp &&

                _tiles[dp[i]].nm == nm) {

                tl[k++] = i;

            }

        }

        if (abs(tl[2]-tl[0]) == 2) {

            return 1;

        }

    }

    return 0;

}


int posKong(int n, int tk) {

    int gp = _tiles[n].gp; int nm = _tiles[n].nm;

int dp[144],tl[4],cnt = 0;

    int i,j,k; j = 0; for (i=0; i<144; i++) {

        if (_tiles[i].tk == tk && i != n) {

            if (_tiles[i].gp == gp &&

                _tiles[i].nm == nm) {

                cnt++;

            }

            dp[j++] = i;

        }

    }

    Bubble(tk,dp,j);

    if (cnt == 3) {

        k = 0; for (i=0; i<j; i++) {

            if (_tiles[dp[i]].gp == gp &&

                _tiles[dp[i]].nm == nm) {

                tl[k++] = i;

            }

        }

        if (abs(tl[2]-tl[0]) == 2) {

            return 1;

        }

    }

    return 0;

}



#2 hunguptodry

    New Member

  • Members
  • PipPip
  • 31 posts

Posted 28 July 2009 - 11:32 AM

the function gets called quite often. several times each gameplay loop.
so i don't think it can be a fluke that the 1st version works.

#3 VilleK

    New Member

  • Members
  • PipPip
  • 10 posts

Posted 28 July 2009 - 03:32 PM

My guess is that when you change the position of that line it affects the stack layout of local variables and that previous to calling this function you have a bug that overwrites the stack (such as a out-of-bounds array access). See if your compiler has the option to build your project in a debug-build with guards against memory overwrites.
ZGameEditor - Develop 64kb games for Windows.
Thrust for Vectrex - ROM-file and 6809 source code.

#4 hunguptodry

    New Member

  • Members
  • PipPip
  • 31 posts

Posted 29 July 2009 - 02:45 AM

yeah its probably stack corruption. maybe, i'll move all the local variables to globals (this is in the heap right?) and see if the problem goes away.
but then I already have a stack version that is fine.
so it'll be hard to conclude anything.
well, at least this is not a show stopper.

#5 AticAtac

    Member

  • Members
  • PipPip
  • 84 posts

Posted 29 July 2009 - 11:05 AM

I am not sure, but you declared t1 with 4 elements:
"...int dp[144],tl[4],cnt = 0; ..."
and in the second loop you are using
"tl[k++] = i;"
and k may have exceed 3, put an assertion before this and make sure that k always be between 0 and 3.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users