Search code examples
c#stack-overflowstack-size

How to change stack size of a console application?


Possible Duplicate:
How to change stack size for a .NET program?

I want to change the stack size for the following console application:

using System;
using System.IO;

class Test {

    static int n;
    static bool[] us;
    static int[,] matr;

    static void dfs(int a) {
        us[a] = true;
        for (int b = 0; b < n; b++) {
            if (!us[b]) {
                dfs(b);
            }
        }
    }

    static void Main() {
        StreamReader input = new StreamReader("input.txt");
        StreamWriter output = new StreamWriter("output.txt");
        string[] snum = input.ReadLine().Split(' ');
        n = int.Parse(snum[0]);      // number of vertices
        int m = int.Parse(snum[1]);  // number of edges
        us = new bool[n];
        matr = new int[n, n];
        for (int i = 0; i < m; i++) {
            snum = input.ReadLine().Split(' ');
            int a = int.Parse(snum[0]) - 1, b = int.Parse(snum[1]) - 1;
            matr[a, b] = matr[b, a] = 1;
        }
        for (int i = 0; i < n; i++) {
            if (!us[i]) {
                dfs(i);
            }
        }
        input.Close();
        output.Close();
    }
}

When n is aprox. 100,000, the depth of dfs is aprox. 100,000 and the application throws a StackOverflowException.

I know that the default stack size is 1 MB, but I do not know how to change it.


Solution

  • int stackSize = 1024*1024*64;
    Thread th  = new Thread( ()=>
        {
            //YourCode
        },
        stackSize);
    
    th.Start();
    th.Join();