2 Replies Latest reply: Jan 27, 2010 4:00 AM by jduprez RSS

    Exception in thread "main" java.lang.StackOverflowError

    807580
      hello
      I write Java code for multiply two huge integer
      If the array longer than two i have error
      Exception in thread "main" java.lang.StackOverflowError
      at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(SingleByteEncoder.java:91)

      does the linked list will solve such a problem?
      my function is
      ------------------
      /////******************************************
      static int[] mult(int[]a,int[] b)
      {
      if(a.length>b.length)
      b= padArray(b,a.length);
      if(a.length<b.length)
      a= padArray(a,b.length);

      int f;
      f= a.length;
      int[] c,result;
      c= new int [1];
      // result=new int[100];
      c[0]=a[0]*b[0];
      if(f==1)
      { System.out.print("hello"+ c[0]+"\n");
      return c;}
      else if(a.length==2&&a[0]==0&b[0]==0)
      {
      c[0]=a[1]*b[1];
      return c;
      }
      else if(a.length==3&&a[0]==0&b[0]==0&&a[1]==0&&b[1]==0)
      {
      c[0]=a[2]*b[2];
      return c;
      }
      else
      {
      int[] aR,aL,bR,bL,X1,X2,X3;
      if(f%2==0)
      {
      aR=new int[f/2];///+1
      aL=new int[f/2];
      bR=new int[f/2];
      bL=new int[f/2];

      }
      else
      {
      aR=new int[f/2+1];///+1
      aL=new int[f/2];
      bR=new int[f/2+1];
      bL=new int[f/2];

      }

      X1=new int[f];
      X2=new int[f];
      X3=new int[f*2];

      aL=SubArray(a,0,f/2);
      aR=SubArray(a,f/2,f);
      bL=SubArray(b,0,f/2);
      bR=SubArray(b,f/2,f);
      X1=mult(aL,bL);
      X2=mult(aR,bR);
      int[] t1,t2,t3,t4;
      t1=new int [aR.length];
      t2=new int [aR.length];
      t1=Add(aL,aR);
      t2=Add(bL,bR);

      X3=mult(t1,t2);

      // result=Add(Add(Shift(X1,f),Shift(Sub(Sub(X3,X1),X2),f/2)),X2);
      int[] w,s;
      s=new int[X1.length+1];

      w=new int[X3.length+1];
      s=Add(X1,X2);
      w=Sub(X3,s);
      result=new int[f*2];
      result=Add(Add(Shift(X1,f),Shift(w,f/2)),X2);


      System.out.print("result is ");
      for(int t=0;t<result.length;t++)
      System.out.print(result[t]);
      return result;
      }

      }


      ---------------
      thanks in advance
        • 1. Re: Exception in thread "main" java.lang.StackOverflowError
          807580
          Everytime you make a method call it gets placed on a stack. Make too many method calls and you run out of memory and get a stack overflow. When this occurs in conjunction with recursion it means that your case for exiting the recursion is wrong and is never reached.
          • 2. Re: Exception in thread "main" java.lang.StackOverflowError
            jduprez
            N.B.: please use code tags, or the CODE button, when posting code. And please always use and review the preview pane before posting at all.

            Flounder nailed it.
            In particular, I would specifically focus on this part:
            X1=mult(aL,bL);
            X2=mult(aR,bR);
            ...
            X3=mult(t1,t2);
            These recursive calls seem not to be included in a conditional branch, so I don't see what would stop the recursion (I could be more affirmative if you had properly formatted the code extract).

            Additionally:
            aL=SubArray(a,0,f/2);
            aR=SubArray(a,f/2,f);
            bL=SubArray(b,0,f/2);
            bR=SubArray(b,f/2,f);
            We don't know your method SubArray(...) (by convention it should be subArray(...) instead), but please note that:
            1) the middle index at f/2 is specified in both left and right parts. This may or may not be what you expect, depending on how subArray handles the upper index (inclusive or not).
            2) if the initial array length is odd, the integer division f/2 rounds down, this may or may not be what you expect

            Edited by: jduprez on Jan 27, 2010 9:59 AM