StackOverflowError computing factorial of a BigInteger?

The problem here looks like its a stack overflow from too much recursion (5000 recursive calls looks like about the right number of calls to blow out a Java call stack) and not a limitation of BigInteger. Rewriting the factorial function iteratively should fix this. For example: public static BigInteger factorial(BigInteger n) { BigInteger result … Read more

JSON.NET StackOverflowException while serialization

The reason you are getting the stackoverflow exception is that Json.NET is a recursive, single-pass tree or graph serializer that, when PreserveReferencesHandling.Objects is enabled, always serializes the first occurrence of each object. You have constructed your 15,000 element Chacha [] array so that the first entry is the head of a linked list containing all … Read more

Large 2D array gives segmentation fault

If your program looks like this … int main(int, char **) { double x[5000][500],y[5000][500],z[5000][500]; // … return 0; } … then you are overflowing the stack. The fastest way to fix this is to add the word static. int main(int, char **) { static double x[5000][500],y[5000][500],z[5000][500]; // … return 0; } The second fastest way … Read more

stackoverflow error in class constructor

The most common cause of StackoverflowExceptions is to unknowingly have recursion, and is that happening here? … public Name(String name) { Name employeeName = new Name(name); // **** YIKES!! *** this.name = employeeName; } Bingo: recursion! This constructor will create a new Name object whose constructor will create a new Name object whose constructor will… … Read more