Segfault Woes

• 04-04-2008
Blurr
Segfault Woes
I keep getting a segfault with the following code, the only thing I have been able to gather is that I always get the segfault when "val" is 2.

Quote:

Code:

```int efficient[6]; extern const int gridsize = 14; extern int grid[gridsize][gridsize]; [. . .]     for (int z = 0; z < 6; z++) // Theoretically runs through all possibilities       {         for (int x = 0; x < gridsize; x++)           for (int y = 0; y < gridsize; y++)             if (grid[x][y] == z)               theoFlood(x, y, z + 1);       } [. . .] void theoFlood(int x, int y, int val)   {     efficient[val - 1]++;         if (x)       if (grid[x-1][y] == val)         theoFlood(x-1, y, val);     if (y)       if (grid[x][y-1] == val)         theoFlood(x, y-1, val);     if (grid[x+1][y] == val)       theoFlood(x+1, y, val);     if (grid[x][y+1] == val)       theoFlood(x, y+1, val);   }```
• 04-04-2008
MuzicMedia
only 2 members
Hey Blurr can you help me with a function?
• 04-04-2008
swoopy
> if (grid[x+1][y] == val)
When x is 13, x+1 will equal 14, which is outside the bounds of your array.

> if (grid[x][y+1] == val)
Same here for y+1.
• 04-05-2008
Elysia
http://cpwiki.sf.net/Buffer_overrun
Do you normal arrays? If so, consider using std::vector and using the .at member function which will throw if out-of-bounds access is made.
• 04-05-2008
Blurr
I'll read up on vectors, thanks. But for now, I've changed the code to

Code:

```void theoFlood(int x, int y, int val)   {     efficient[val - 1]++;     if (x)       if (grid[x-1][y] == val)         theoFlood(x-1, y, val);     if (y)       if (grid[x][y-1] == val)         theoFlood(x, y-1, val);     if (x < gridsize)       if (grid[x+1][y] == val)         theoFlood(x+1, y, val);     if (y < gridsize)       if (grid[x][y+1] == val)         theoFlood(x, y+1, val);   }```
The only variable I'm trying to write here is efficient, which doesn't seem like it will ever go out of bounds. Anyone know why it will crash only when "val" = 2, and not 1?
• 04-05-2008
Blurr
Quote:

Originally Posted by MuzicMedia
Hey Blurr can you help me with a function?

• 04-05-2008
Elysia
Most likely another out of bounds. Grid is another array, just as efficient. So turn them into vector and use .at instead of []. Then see if you catch any out of bounds.
• 04-05-2008
Swordsalot
maybe more of the grid is '2' than 1.

It looks to me like there's nothing stopping the recursive theoflood from continuously searching into negative values of x (or y). If it keeps hitting 'val' in the theoFlood, it will get closer and closer to negatives, eventually reaching theoFlood(-1....) and trying to access memory that it shouldn't be (at grid[-1][...]).
• 04-05-2008
Blurr
Quote:

Originally Posted by Swordsalot
maybe more of the grid is '2' than 1.

It looks to me like there's nothing stopping the recursive theoflood from continuously searching into negative values of x (or y). If it keeps hitting 'val' in the theoFlood, it will get closer and closer to negatives, eventually reaching theoFlood(-1....) and trying to access memory that it shouldn't be (at grid[-1][...]).

Isn't that stopped with

> if (x)

and

> if (y)
?

Elysia: Will do
• 04-05-2008
Blurr
When I try to catch out_of_range g++ says that that it has not been declared. Any clues as to how I'd fix it?
• 04-05-2008
Elysia
It's called std::out_of_rage.
In header file stdexcept. Did you include that too?
• 04-05-2008
swoopy
Code:

```>    if (x < gridsize) >      if (grid[x+1][y] == val) >        theoFlood(x+1, y, val);```
You step out-of-bounds here. Assume x=13, then x+1=14. The indices of grid[] run from 0 to 13. grid[14] is one past the end.
• 04-08-2008
Blurr
It was that combined with infinite recursion. I've fixed it. Thanks for the help guys!