title: Using DFS algorithm to flood-fill a shape
body: <p>I am working on a project that must be something like windows's paint. I've already implemented 8 tools (which are brush, rectangle, oval, polygon, triangle, line, spray and filler tool). Now I want to make a "bucket" tool which must fill the area around itself. 
  I use DFS algorithm for this tool but when the area is large, gdb give below errors:</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0xb7c47f9d in _IO_new_file_xsputn (f=0xb7d82ac0 &lt;_IO_2_1_stdout_&gt;, data=0xbf80009e, n=6)
      at fileops.c:1273
  1273    fileops.c: No such file or directory.
  </code></pre>
  
  <p>Does anyone know what does this error mean?
  You can see bucket.h and bucket.cpp below:</p>
  
  <p>bucket.h:</p>
  
  <pre><code>#ifndef BUCKET_H
  #define BUCKET_H
  
  #include "tool.h"
  
  #include "SDL/SDL.h"
  #include "SDL/SDL_gfxPrimitives.h"
  
  #include &lt;cmath&gt;
  
  class Bucket : public Tool {
      private:
          bool **mark;
          Color selectedPointColor;
      public:
          Bucket( bool state, SDLKey key ) : Tool( state, key ) {}
  
          virtual void draw( SDL_Surface*, int, int, int, int, int, Color color );
  
          friend void DFS( SDL_Surface*, int, int, Color, bool** );
          friend Color getColor( SDL_Surface*, int, int );
  };
  
  inline Color getColor( SDL_Surface *screen, int x, int y ){
      Uint32* pixel = (Uint32*) screen-&gt;pixels;
      Uint8* color = (Uint8*) &amp;( pixel[ y * screen-&gt;w + x ] );
      return Color( (int) color[2], (int) color[1], (int) color[0] );
  }
  
  inline void DFS( SDL_Surface *screen, int x, int y, Color color, Color selectedPointColor ){
      static int counter;
      counter++;
      cout &lt;&lt; counter &lt;&lt; endl;
      pixelRGBA( screen, x, y, color.red(), color.green(), color.blue(), 255 );
  
      if ( x + 1 &lt; screen-&gt;w &amp;&amp; getColor( screen, x + 1, y ) == selectedPointColor )
          DFS( screen, x + 1, y, color, selectedPointColor );
  
      if ( y + 1 &lt; screen-&gt;h &amp;&amp; getColor( screen, x, y + 1 ) == selectedPointColor )
          DFS( screen, x, y + 1, color, selectedPointColor );
  
      if ( x - 1 &gt;= 0 &amp;&amp; getColor( screen, x - 1, y) == selectedPointColor )
          DFS( screen, x - 1, y, color, selectedPointColor );
  
      if ( y - 1 &gt;= 0 &amp;&amp; getColor( screen, x, y - 1) == selectedPointColor )
          DFS( screen, x, y - 1, color, selectedPointColor );
  }
  
  #endif
  </code></pre>
  
  <p>bucket.cpp: </p>
  
  <pre><code>#include "bucket.h"
  
  void Bucket::draw( SDL_Surface *screen, int x, int y, int, int, int, Color color ){
      selectedPointColor = getColor( screen, x, y );
      if ( selectedPointColor == color )
          return;
      DFS( screen, x, y, color, this-&gt;selectedPointColor );
  }
  </code></pre>
  
  <p>any help would be appreciated.</p>