0

I write a DFS(depth first search) founction in my C# project A, and it runs OK. Then I build a new c# project B, which has more lines of codes than A. When I run the same founction in project B with the same input data,my VS2008 shows that there is a stack-overflow error.

Can I change the size of Stack in C#?

The founction's name is :FindBlocksFounction().

Codes which cause stack-overflow:

int tempx = nowx + dir[i, 0]; 
int tempy = nowy + dir[i, 1];
if (tempx < 0 || tempy < 0 || tempx >= m_Bitmap.Height || tempy >= m_Bitmap.Width)
    continue;
int next;
next = PointList.FindIndex(t =>
{
    if (t.x == tempx && t.y == tempy)
        return true;
    else
         return false;
 });//It seems like that FindIndex() in List<> costs some stack room.
if (next == -1)
    continue;

if (color[next] == 0)
{
    FindBlocksFounction(next);
}
Wendy Pan
  • 34
  • 7
  • 5
    Can you show us the code of the method that causes the problem? Maybe it can be fixed. Increasing the stack size will only prolong your agony. – Tudor Mar 31 '12 at 07:25

1 Answers1

2

I think best way to convert your Depth First Search algorithm from recursion to using Queue/Dequeue collection. It is a not complex task. Just google it or take a look here:
Non recursive Depth first search algorithm

It will prevent your code from stack size issues for any amount of data.

Community
  • 1
  • 1
asktomsk
  • 2,289
  • 16
  • 23