title: Queue throwing out of memory exception in flood fill
body: <p>I'm doing a flood fill on a 2000 x 2000 x 256 bitmap (in memory) using the code from here: <a href="http://rosettacode.org/wiki/Bitmap/Flood_fill#C.23" rel="nofollow">http://rosettacode.org/wiki/Bitmap/Flood_fill#C.23</a></p>
  
  <p>The Queue is throwing an out of memory error when q Count = 33554432 (~33MB). Obviously, I've got plenty more memory than that. I've also created a struct of IntPoint so each point is 2 ints instead of 2 doubles.</p>
  
  <ol>
  <li>Is there a way to manually allocate more memory for the Queue? </li>
  <li>If that isn't possible, does anybody have any other suggestions?</li>
  </ol>
  
  <p>FYI: I doubt if this is important, but I'm actually doing the flood fill on a 2000 x 2000 array of bytes in memory as opposed to actually loading a bitmap, checking colors and replacing.</p>
  
  <p>Below is my implementation of the flood fill from the above referenced link (maybe a glaring error will jump out):</p>
  
  <pre><code>public struct IntPoint
      {
          public int X, Y;
  
          public IntPoint(int p1, int p2)
          {
              X = p1;
              Y = p2;
          }
      }
  
  
      public void TerrainFloodFill(Point Tpt, byte targetElevation, byte replacementTerrain)
      {
          IntPoint pt = new IntPoint((int)Tpt.X, (int)Tpt.Y);
  
          Queue&lt;IntPoint&gt; q = new Queue&lt;IntPoint&gt;();
          q.Enqueue(pt);
          while (q.Count &gt; 0)
          {
              IntPoint n = q.Dequeue();
              if (HeightMap[(int)n.X, (int)n.Y] != targetElevation)
                  continue;
              IntPoint w = n, e = new IntPoint(n.X + 1, n.Y);
              while ((w.X &gt; 0) &amp;&amp; HeightMap[(int)w.X, (int)w.Y] == targetElevation)
              {
                  TerrainMap[(int)w.X, (int)w.Y] = replacementTerrain;
                  if ((w.Y &gt; 0) &amp;&amp; HeightMap[(int)w.X, (int)w.Y - 1] == targetElevation)
                      q.Enqueue(new IntPoint(w.X, w.Y - 1));
                  if ((w.Y &lt; MapHeight - 1) &amp;&amp; HeightMap[(int)w.X, (int)w.Y + 1] == targetElevation)
                      q.Enqueue(new IntPoint(w.X, w.Y + 1));
                  w.X--;
              }
              while ((e.X &lt; MapWidth - 1) &amp;&amp; HeightMap[(int)e.X, (int)e.Y] == targetElevation)
              {
                  TerrainMap[(int)e.X, (int)e.Y] = replacementTerrain;
                  if ((e.Y &gt; 0) &amp;&amp; HeightMap[(int)e.X, (int)e.Y - 1] == targetElevation)
                      q.Enqueue(new IntPoint(e.X, e.Y - 1));
                  if ((e.Y &lt; MapHeight - 1) &amp;&amp; HeightMap[(int)e.X, (int)e.Y + 1] == targetElevation)
                      q.Enqueue(new IntPoint(e.X, e.Y + 1));
                  e.X++;
              }
          }
  
      }
  </code></pre>
  
  <p>As always, thanks in advance for the help!</p>