TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Shortest Crashing C Program

270 点作者 cfj将近 12 年前

23 条评论

AlexanderDhoore将近 12 年前
This reminds me of "A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux" [1]. The author tries to create the smallest possible elf executable possible. You would think it'd be easy... :) go read it. Very cool!<p>[1] <a href="http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html" rel="nofollow">http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm...</a>
评论 #5765019 未加载
femto将近 12 年前
It depends on the definition. You can do better than this if you define a valid C program as anything that passes though the C compiler and generates an executable. Behold the zero length program:<p>$ touch a.c<p>$ gcc -c a.c<p>$ ld a.o<p>ld: warning: cannot find entry symbol _start; defaulting to 0000000000400078<p>$ ./a.out<p>Segmentation fault
评论 #5762578 未加载
评论 #5762285 未加载
评论 #5762497 未加载
评论 #5766040 未加载
评论 #5762491 未加载
评论 #5762746 未加载
to3m将近 12 年前
The explanation is not quite correct - execution starts at &#38;main rather than the address given by the value of main. On VC++, at least - well, on my PC anyway - the process halts because the data segment doesn't have the execute bit set. It isn't trying to run code at address 0.<p>(If execution of bytes in the data segment were possible, which I'm sure it used to be, then you'd still likely get a crash, but it's not guaranteed. (uint32_t)0 is a valid sequence of instructions - it's ADD BYTE PTR [EAX],AL - and so if EAX contained a valid value then it would execute without a problem. Then, if the following byte were 0xC3 (RET) then the program would execute. OK, so that's all rather unlikely, but you have to bear these things in mind. So I think 0xCC (INT 3) would be a better choice.)
评论 #5763727 未加载
评论 #5762971 未加载
dysoco将近 12 年前
Seems to work really well: It even crashed the website.
评论 #5762771 未加载
评论 #5762306 未加载
评论 #5762265 未加载
Jabbles将近 12 年前
Who says it will crash? Could run very nicely, printing a list of prime numbers, or write poetry, or anything else that undefined behaviour encompasses.
评论 #5764719 未加载
评论 #5766846 未加载
alcuadrado将近 12 年前
It seems to be down, google cache: <a href="http://webcache.googleusercontent.com/search?q=cache:4FhUcns72Z4J:llbit.se/%3Fp%3D1744+&#38;cd=1&#38;hl=es-419&#38;ct=clnk&#38;gl=ar" rel="nofollow">http://webcache.googleusercontent.com/search?q=cache:4FhUcns...</a>
评论 #5762272 未加载
bbanyc将近 12 年前
The first IOCCC winner declared main as a short[] of VAX machine code: <a href="http://www.ioccc.org/1984/mullender.c" rel="nofollow">http://www.ioccc.org/1984/mullender.c</a><p>You could probably do the same thing in x86 and it'd work on a modern compiler.
评论 #5762364 未加载
deweerdt将近 12 年前
&#62; Also, global variables in C are initialized to zero implicitly, so this is equivalent:<p>EDIT: <i>this</i> is wrong, see below.<p>That's wrong. 'static' variables are initialized to zero. Non-static variables are un-initialized, so they have a "random" value.<p>See:<p>$ valgrind ./a.out<p>==5118== Memcheck, a memory error detector<p>==5118== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.<p>==5118== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info<p>==5118== Command: ./a.out<p>==5118==<p>==5118==<p>==5118== Process terminating with default action of signal 11 (SIGSEGV)<p>==5118== Bad permissions for mapped region at address 0x600864<p>==5118== at 0x600864: ??? (in /home/def/a.out)<p>==5118== by 0x4E54A14: (below main) (in /usr/lib/libc-2.17.so)
评论 #5762815 未加载
评论 #5762787 未加载
评论 #5762743 未加载
themattrix将近 12 年前
You can go even shorter if you cheat:<p><pre><code> $ cat short.c M $ gcc -DM='main;' short.c -o short $ ./short Segmentation fault</code></pre>
danielsamuels将近 12 年前
The Shortest Crashing Wordpress Site
_kushagra将近 12 年前
The site seems down, "Error establishing a database connection"
评论 #5762365 未加载
jstanley将近 12 年前
I'm not convinced this is a C89 program. It is only an "accident" that the linker doesn't know about types.<p>I find it hard to believe that the C89 spec states that an integer called "main" is to be considered the main function, and suspect this is undefined behaviour (though I've not checked).
评论 #5762274 未加载
评论 #5762348 未加载
marshray将近 12 年前
How about:<p><pre><code> main(){*(int*)0=0;} </code></pre> or:<p><pre><code> main(){*""=0;} </code></pre> or:<p><pre><code> main(){main();}</code></pre>
评论 #5762504 未加载
评论 #5762545 未加载
评论 #5762640 未加载
joeyh将近 12 年前
Seems appropriate that the default C program, as it were, segfaults.
stinos将近 12 年前
"address 0, which is not an address that we have access to"<p>if I'm not mistaken, there are platforms like TI C600 dsps for which 0 is the start of the usable address space
sfvisser将近 12 年前
Interesting. We tried to do the same for Haskell. The shortest we could come up with:<p>import Unsafe.Coerce;main=unsafeCoerce()1
评论 #5763473 未加载
webreac将近 12 年前
With visual studio V6.0 (AFAIR), I made a short program that crashed the compiler:<p>int a;::a::b();
sbanach将近 12 年前
It's also the shortest C program that you can link at all.
评论 #5762637 未加载
qznc将近 12 年前
"The shortest crashing C89 program" to be more precise. :)
Trufa将近 12 年前
And interesting question, what would be the shortest not crashing C program?<p>main(){}<p>??
评论 #5763114 未加载
hkmurakami将近 12 年前
reminds me of the recent fad of TAS (tool assisted speed run) videos of "fastest crash" of video games.
stefap2将近 12 年前
Shortest crashing website: Error establishing a database connection
kghose将近 12 年前
for those who see the server crashing: The program is in C89<p>main;