Pushing each parameter into the stack (the order diverges and depends on each compiler, also some of them are sometimes stored on the CPU registers for performance improvements)
打真正的电话。
So, usually this takes a few bytes depeding on the number and type of the parameters as well as the machine architecture.
Considering this was tagged with "hacking", I suspect the "stack overflow" he's referring to is a call stack overflow, rather than a higher level stack overflow such as those referenced in most other answers here. It doesn't really apply to any managed or interpreted environments such as .NET, Java, Python, Perl, PHP, etc, which web apps are typically written in, so your only risk is the web server itself, which is probably written in C or C++.
堆栈溢出是指堆栈使用的内存超过了程序应该使用的内存。在嵌入式系统中,堆栈可能只有256个字节,如果每个函数占用32个字节,那么你只能让函数调用8 deep-function 1调用 function 2谁调用 function 3谁调用 function 4..。调用函数8调用函数9但是函数9覆盖堆栈外的内存。这可能会覆盖内存、代码等。
Many programmers make this mistake by calling function A that then calls function B, that then calls function C, that then calls function A. It might work most of the time, but just once the wrong input will cause it to go in that circle forever until the computer recognizes that the stack is overblown.
Interrupts don't call secondary functions - they set flags, copy data, and let the application take care of processing it (otherwise you might get 8 deep in your function call tree, have an interrupt, and then go out another few functions inside the interrupt, causing the blowout). You have several call trees - one for the main processes, and one for each interrupt. If your interrupts can interrupt each other... well, there be dragons...
不要把你的程序分解成越来越小的函数——即使不计算本地变量,每个函数调用在堆栈上也要消耗64个字节(32位处理器,节省了一半的 CPU 寄存器、标志等)
保持调用树浅(类似于上面的语句)
网络服务器
It depends on the 'sandbox' you have whether you can control or even see the stack. Chances are good you can treat web servers as you would any other high level language and operating system - it's largely out of your hands, but check the language and server stack you're using. It 是 possible to blow the stack on your SQL server, for instance.
package com.company.dynamicProgramming;
import java.math.BigInteger;
public class FibonacciByBigDecimal {
public static void main(String ...args) {
int n = 100000;
BigInteger[] fibOfnS = new BigInteger[n + 1];
System.out.println("fibonacci of "+ n + " is : " + fibByDivCon(n, fibOfnS));
}
static BigInteger fibByDivCon(int n, BigInteger[] fibOfnS){
if(fibOfnS[n]!=null){
return fibOfnS[n];
}
if (n == 1 || n== 2){
fibOfnS[n] = BigInteger.ONE;
return BigInteger.ONE;
}
// creates 2 further entries in stack
BigInteger fibOfn = fibByDivCon(n-1, fibOfnS).add( fibByDivCon(n-2, fibOfnS)) ;
fibOfnS[n] = fibOfn;
return fibOfn;
}
}
当我运行 n = 100,000的代码时,结果如下-
Exception in thread "main" java.lang.StackOverflowError
at com.company.dynamicProgramming.FibonacciByBigDecimal.fibByDivCon(FibonacciByBigDecimal.java:29)
at com.company.dynamicProgramming.FibonacciByBigDecimal.fibByDivCon(FibonacciByBigDecimal.java:29)
at com.company.dynamicProgramming.FibonacciByBigDecimal.fibByDivCon(FibonacciByBigDecimal.java:29)
上面你可以看到创建了 StackOverflow 错误。现在的原因是太多的递归作为-
// creates 2 further entries in stack
BigInteger fibOfn = fibByDivCon(n-1, fibOfnS).add( fibByDivCon(n-2, fibOfnS)) ;
因此,堆栈中的每个条目又创建了2个条目,以此类推... ... 它被表示为-
最终会创建如此多的条目,以至于系统无法在堆栈中处理并抛出 StackOverlowError。
预防:
对于上面的例子透视图-
1.避免使用递归方法或再次减少/限制递归一级除法,比如如果 n 太大,那么将 n 分割,以便系统能够处理其极限。
2. Use other approach, like the loop approach I have used in 1st code sample. (I am not at all intended to degrade Divide & Concur or Recursion as they are legendary approaches in many most famous algorithms.. my intention is to limit or stay away from recursion if I suspect stack overflow issues)