There is very simple task I want to do, that somehow makes the program crash.
I have a list of number contained within, all unique. Past a certain number, I want to invert the order.
Example : 5, 16, 11, 3, 8, 4 -> 5, 16, 11, 4, 8, 3 when using 3 as the pivot point.
Below is one of the many methods I tried.
private List<int> ShiftPath(List<int> oldPath, int shift)
{
List <int> newPath = new List<int>();
int counter = 0;
// Start reordering
// Forwards
while (oldPath[counter] != shift)
{
newPath.Add(oldPath[counter]);
counter++;
}
// Backwards
counter = oldPath.Count - 1;
while (oldPath[counter] != shift)
{
newPath.Add(oldPath[counter]);
counter--;
}
// New endpoint
newPath.Add(shift);
// Update
return newPath;
}
Now this works. It may not be the optimal solution, but it works. I've been using this methods for quite a while, but now I've reached a point where the number of items in the list becomes extremely big (above 6,000). Eventually, I get an StackOverFlowException when trying to add something to newPath.
I'm 100% sure there is no infinite loop like VS claims. I've tried other methods such as getting the range of items directly, for and foreach loops instead of while, all crash eventually. It seems that the amount of data is just too big. And it's just going to get bigger (upwards to 20,000).
Proof (?) : even this will make the program throw an exception : List <int> newPath = new List<int>(oldPath);
Any idea on what is causing this/how to fix it?
-From a beginner.