Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I avoid changing the Stack Size AND avoid getting a Stack Overflow in C#
    primarykey
    data
    text
    <p>I've been trying to find an answer to this question for a few hours now on the web and on this site, and I'm not quite there.</p> <p>I understand that .NET allocates 1MB to apps, and that it's best to avoid stack overflow by recoding instead of forcing stack size. </p> <p>I'm working on a "shortest path" app that works great up to about 3000 nodes, at which point it overflows. Here's the method that causes problems:</p> <pre><code> public void findShortestPath(int current, int end, int currentCost) { if (!weight.ContainsKey(current)) { weight.Add(current, currentCost); } Node currentNode = graph[current]; var sortedEdges = (from entry in currentNode.edges orderby entry.Value ascending select entry); foreach (KeyValuePair&lt;int, int&gt; nextNode in sortedEdges) { if (!visited.ContainsKey(nextNode.Key) || !visited[nextNode.Key]) { int nextNodeCost = currentCost + nextNode.Value; if (!weight.ContainsKey(nextNode.Key)) { weight.Add(nextNode.Key, nextNodeCost); } else if (weight[nextNode.Key] &gt; nextNodeCost) { weight[nextNode.Key] = nextNodeCost; } } } visited.Add(current, true); foreach (KeyValuePair&lt;int, int&gt; nextNode in sortedEdges) { if(!visited.ContainsKey(nextNode.Key) || !visited[nextNode.Key]){ findShortestPath(nextNode.Key, end, weight[nextNode.Key]); } } }//findShortestPath </code></pre> <p>For reference, the Node class has one member:</p> <pre><code> public Dictionary&lt;int, int&gt; edges = new Dictionary&lt;int, int&gt;(); </code></pre> <p>graph[] is:</p> <pre><code> private Dictionary&lt;int, Node&gt; graph = new Dictonary&lt;int, Node&gt;(); </code></pre> <p>I've tried to opimize the code so that it isn't carrying any more baggage than needed from one iteration (recursion?) to the next, but with a 100K-Node graph with each node having between 1-9 edges it's going to hit that 1MB limit pretty quickly. </p> <p>Anyway, I'm new to C# and code optimization, if anyone could give me some pointers (<a href="http://xkcd.com/138/" rel="nofollow noreferrer">not like this</a>) I would appreciate it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload