What's the shortest code to cause a stack over

2019-01-08 02:41发布

To commemorate the public launch of Stack Overflow, what's the shortest code to cause a stack overflow? Any language welcome.

ETA: Just to be clear on this question, seeing as I'm an occasional Scheme user: tail-call "recursion" is really iteration, and any solution which can be converted to an iterative solution relatively trivially by a decent compiler won't be counted. :-P

ETA2: I've now selected a “best answer”; see this post for rationale. Thanks to everyone who contributed! :-)

30条回答
Lonely孤独者°
2楼-- · 2019-01-08 03:17

Java

Slightly shorter version of the Java solution.

class X{public static void main(String[]a){main(a);}}
查看更多
SAY GOODBYE
3楼-- · 2019-01-08 03:19

In english:

recursion = n. See recursion.
查看更多
等我变得足够好
4楼-- · 2019-01-08 03:19

How about the following in BASIC:

10 GOSUB 10

(I don't have a BASIC interpreter I'm afraid so that's a guess).

查看更多
Summer. ? 凉城
5楼-- · 2019-01-08 03:21

TeX:

\def~{~.}~

Results in:

! TeX capacity exceeded, sorry [input stack size=5000].
~->~
    .
~->~
    .
~->~
    .
~->~
    .
~->~
    .
~->~
    .
...
<*> \def~{~.}~

LaTeX:

\end\end

Results in:

! TeX capacity exceeded, sorry [input stack size=5000].
\end #1->\csname end#1
                      \endcsname \@checkend {#1}\expandafter \endgroup \if@e...
<*> \end\end
查看更多
SAY GOODBYE
6楼-- · 2019-01-08 03:21

C - It's not the shortest, but it's recursion-free. It's also not portable: it crashes on Solaris, but some alloca() implementations might return an error here (or call malloc()). The call to printf() is necessary.

#include <stdio.h>
#include <alloca.h>
#include <sys/resource.h>
int main(int argc, char *argv[]) {
    struct rlimit rl = {0};
    getrlimit(RLIMIT_STACK, &rl);
    (void) alloca(rl.rlim_cur);
    printf("Goodbye, world\n");
    return 0;
}
查看更多
萌系小妹纸
7楼-- · 2019-01-08 03:22

Here's another interesting one from Scheme:

((lambda (x) (x x)) (lambda (x) (x x)))
查看更多
登录 后发表回答