How many function calls will a stack overflow cause

Hi, android / java developers,

When a function calls a function, and this function calls another, etc., how many calls (stack length) will lead me to the stack through the stream? Is there a general rule?

The reason I ask is because I am now more efficient (design wise) for my 5-player card game.

Solution 1:

for(int i=0;i<100;i++){
         p1.play();
         p2.play();
         p3.play();
         p4.play();
}

Solution 2:

   p1.play();    //where p1.play() calls p2.play() and so on until p4 calls p1 again.   
                 // this will go on for 100 times

I prefer solution 2, so if there is a failure, I see all the function calls from p1 with i = 0 to p4 with i = 100

but with solution 1, the stack is much shorter, but when the crash occurs, I will see at the beginning of the loops the play () function being called where the crash occurred

What are you offering? I know that these are 2 questions in 1, but they are very related.

Thank you all

+5
source share
6

, Java . . .

- IMO, ... . , N = 100 ( ) , , ( ) . .

, a (),

. try catch, ?

+3

, . , .

, , . () , , , , . , , , , . , : http://developer.android.com/tools/debugging/index.html

+2

, Shivan Dragon , , . :

public void stackTest(int iteration)
{
    System.out.println("Iteration: "+iteration); // or Log
    stackTest(iteration+1);
}

:

stackTest(1);

, .

+2

, , x . , , .., , () .

, , , , . .

+1

frame Java : . , . - JVM Code max_stack:

Code_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 max_stack;
    u2 max_locals;
    u4 code_length;
    u1 code[code_length];
    u2 exception_table_length;
    {   u2 start_pc;
        u2 end_pc;
        u2 handler_pc;
        u2 catch_type;
    } exception_table[exception_table_length];
    u2 attributes_count;
    attribute_info attributes[attributes_count];
}

JVM StackOverflowException. JVM:

Java: Java, , Java StackOverflowError. Java , , Java , Java OutOfMemoryError.

: , JVM. / . , . , OutOfMemoryException , , .

0

.

; . , .

, /.

Here is a topic on this topic: Recursion or iteration?

0
source

All Articles