Stack overflow occurs on the first call to a procedure inside a recursive procedure

I get an error due to an error that I understand, but I don’t understand why the stack overflow occurs in the first procedure in a recursive procedure, and not when the recursive procedure is called.

In the sudoku puzzle solving method, there is a recursive segment here (bold text is a recursive call:


    System.out.print(""); &lt= Qaru occurs here
    int[] move_quality_sorted_keys = Sorting_jsc.RadixSort_unsigned_1( move_quality );
    for( int xPossibleMove = 1; xPossibleMove <= ctPossibleMoves; xPossibleMove++ ){
        int xMove = move_quality_sorted_keys[ctPossibleMoves - xPossibleMove + 1];
        int[][] new_grid = new int[10][10];
        for( int xRow = 1; xRow <= 9; xRow++ )
            for( int xColumn = 1; xColumn <= 9; xColumn++ )
                new_grid[xRow][xColumn] = grid[xRow][xColumn];
        new_grid[move_row[xMove]][move_column[xMove]] = move_value[xMove];
        int[][] solution = solveSudokuGrid( new_grid );
        if( solution != null ) return solution;
    }

Error (note that this happens in the System.out.print () statement):

Exception in thread "main" java.lang.StackOverflowError
    at java.io.BufferedWriter.write(BufferedWriter.java:221)
    at java.io.Writer.write(Writer.java:157)
    at java.io.PrintStream.write(PrintStream.java:525)
    at java.io.PrintStream.print(PrintStream.java:669)
    at Euler100.solveSudokuGrid(Euler100.java:2458)
    at Euler100.solveSudokuGrid(Euler100.java:2467)
    at Euler100.solveSudokuGrid(Euler100.java:2467)
    at Euler100.solveSudokuGrid(Euler100.java:2467)
    at Euler100.solveSudokuGrid(Euler100.java:2467)
    at Euler100.solveSudokuGrid(Euler100.java:2467)
    at Euler100.solveSudokuGrid(Euler100.java:2467)
    at Euler100.solveSudokuGrid(Euler100.java:2467)
    at Euler100.solveSudokuGrid(Euler100.java:2467)
    at Euler100.solveSudokuGrid(Euler100.java:2467)

I expect the stack to overflow when the SudokuGrid solution is called, and not in the print statement. Why?

+4
source share
2 answers

: , System.out.println, 4 ( ) , . , . , :

  • , 1
  • println, 5
  • , 2
  • println, 6
  • , 3
  • println, 7
  • ...
  • , n
  • println, n + 4
  • , n + 1
  • ...

, ( , , , ), , println .

, , - , , . , ( ), , , println , , radix, , . -, , ( : 6 , ).

+5

. wiki

, System.out.println BufferedWriter.write, , stackoverflow.

0

Source: https://habr.com/ru/post/1531205/


All Articles