Crash on my Flood Fill algorithm

I'm working on a map editor for my game. I have designed a flood fill algorithm. It was working fine until last night. I added some new features, then discovered it suddenly decided to crash my program. When I debug it, It crashes on line 5. However, it only crashes the program when I flood fill something large, like the entire map(which is 256 by 256). Here is the code for the Flood function:

1
2
3
4
5
6
7
8
9
10
11
void Flood(int mx, int my, unsigned char selid, unsigned char orig, unsigned char map[256][256]) {
     if(mx >= 0 && mx <= 255 && my >= 0 && my <= 255) {
          if(map[mx][my] == orig) {
               map[mx][my] = selid;
               Flood(mx - 1, my, selid, orig, map);         
               Flood(mx + 1, my, selid, orig, map);         
               Flood(mx, my - 1, selid, orig, map);         
               Flood(mx, my + 1, selid, orig, map); 
          }       
     }
}


Any help will be appreciated. Thanks in advance.
Last edited on
Have you run out of stack?
I think that may be the case... I have no idea what options I have, however. :/
You'll infinitely recur on that. Flood(mx, my, ...) will call Flood(mx + 1, my, ...) which will call Flood(mx + 1 - 1, my, ...) and you will repeat forever.
Wouldn't that be avoided by the if(map[mx][my] == orig) check? Since mx + 1 -1 was already changed to selid, that should return false; therefore ending the function.

I could be wrong, however.

EDIT:

Plus the fact that when filling small spaces it works perfectly fine.
Last edited on
Google "non-recursive flood fill algorithm". There are many published solutions out there.
You could use an stack or a queue (changing DFS to BFS) to store the non-processed cells.
Your code is using as much as rows+columns of depth
Topic archived. No new replies allowed.