Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Shortest code that returns Segmentation Fault (SIGSEGV) in any programming language.

share|improve this question

22 Answers 22

up vote 26 down vote accepted

C, 5 characters

main;

It's variable declaration - int type is implied (feature copied from B language) and 0 is default value. When executed this tries to execute a number (numbers aren't executable), and causes SIGSEGV.

share|improve this answer
    
Default value may not be 0, and this may give a BUS error rather than a segmentation fault. –  Macmade Aug 10 '13 at 8:25
1  
@Macmade: Actually, it is 0. static variables start as 0, and main; is static, as I declared it outside function. c-faq.com/decl/initval.html –  xfix Aug 16 '13 at 8:20
2  
last time i played with this thing, i figured out that there's a different reason for the segfault. First of all by calling main you jump to the location of main, not the value, another thing is main is an int, it's located in .bss, usually functions are located in .text, when the kernel loads the elf program it creates an executable page for .text and non-executable for .bss, so by calling main, you jump to a non-executable page, and execution something on a such page is a protection fault. –  mniip Dec 6 '13 at 17:55
    
Yep, segfaults in C are pretty much the default :P –  Paul Draper May 24 at 23:23

Bash, 11      

kill -11 $$
share|improve this answer
2  
+1 for thinking different. –  FUZxxl Dec 26 '11 at 22:19
1  
Signal 11 in 11 characters. Seems legit. –  nyuszika7h Dec 31 '13 at 12:39

3 Characters in Assembly

RET

writing only this will give Segmentation Fault.

share|improve this answer
1  
As an MSDOS .com file, it runs and terminates without error. –  J B Dec 26 '11 at 19:10
    
see this code http://ideone.com/RMuLf –  Amol Sharma Dec 26 '11 at 19:46
3  
My point being: just specifying “assembly” isn't enough to make it segfault. –  J B Dec 26 '11 at 19:56
    
'ret' by itself should be a valid program; I suspect that is an error in the emulator. –  Sir_Lagsalot Jan 20 '12 at 16:19
12  
@JB: On MS DOS, no program will ever produce a segmentation fault. That's because MS DOS runs in real mode where memory protection is nonexistent. –  celtschk Feb 4 '12 at 17:25

pdfTeX (51)

\def~#1{\meaning}\write0{\expandafter~\string}\bye

This is actually probably a bug, but it is not present in the original TeX, written by Knuth: compiling the code with tex filename.tex instead of pdftex filename.tex does not produce a segfault.

share|improve this answer

C - 11(19) 7(15) 6(14) 1 chars, AT&T x86 assembler - 8(24) chars

C version is:

*(int*)0=0;

The whole program (not quite ISO-compliant, let's assume it's K&R C) is 19 chars long:

main(){*(int*)0=0;}

Assembler variant:

orl $0,0

The whole program is 24 chars long (just for evaluation, since it's not actually assembler):

main(){asm("orl $0,0");}

EDIT:

A couple of C variants. The first one uses zero-initialization of global pointer variable:

*p;main(){*p=0;}

The second one uses infinite recursion:

main(){main();}

The last variant is the shortest one - 7(15) characters.

EDIT 2:

Invented one more variant which is shorter than any of above - 6(14) chars. It assumes that literal strings are put into a read-only segment.

main(){*""=0;}

EDIT 3:

And my last try - 1 character long:

P

Just compile it like that:

cc -o segv -DP="main(){main();}" segv.c
share|improve this answer
3  
in C isn't main; only 5 charecters –  Arya Dec 26 '11 at 10:50
    
@Arya: Huh? What do you mean? –  Alexander Bakulin Dec 26 '11 at 11:01
    
only main;in file.c –  Arya Dec 26 '11 at 12:18
    
:Linker doesn't check whether main is function or not .It just pass it to the loader and return sigsegv –  Arya Dec 26 '11 at 12:24
2  
Of course the last entry can be used for everything, you just have to supply the right compiler arguments. Which should make it the automatic winner of any code golf contest. :-) –  celtschk Feb 4 '12 at 17:20

C, 18

main(){raise(11);}
share|improve this answer

Perl, 10 / 12 chars

A slightly cheatish solution is to shave one char off Joey Adams' bash trick:

kill 11,$$

However, to get a real segfault in Perl, unpack p is the obvious solution:

unpack p,1x8

Technically, this isn't guaranteed to segfault, since the address 0x31313131 (or 0x3131313131313131 on 64-bit systems) just might point to valid address space by chance. But the odds are against it. Also, if perl is ever ported to platforms where pointers are longer than 64 bits, the x8 will need to be increased.

share|improve this answer

Python 33

import os
os.kill(os.getpid(),11)

Sending signal 11 (SIGSEGV) in python.

share|improve this answer
    
Also 33 characters: from os import* and kill(getpid(),11) –  Timtech Jan 8 at 15:45

Perl ( < 5.14 ), 9 chars

/(?{??})/

In 5.14 the regex engine was made reentrant so that it could not be crashed in this way, but 5.12 and earlier will segfault if you try this.

share|improve this answer
    
I can reproduce this on Perl 5.14 (Debian) and 5.18 (Arch Linux). sprunge.us/RKHT –  nyuszika7h Jan 21 at 21:51

19 characters in C

main(a){*(&a-1)=1;}

It corrupts return address value of main function, so it gets a SIGSEGV on return of main.

share|improve this answer
    
It depends on the stack frame layout, so in some architecture it can possibly not fail. –  Alexander Bakulin Dec 27 '11 at 19:41

Haskell, 31

foreign import ccall main::IO()

This produces a segfault when compiled with GHC and run. No extension flags are needed, as the Foreign Function Interface is in the Haskell 2010 standard.

share|improve this answer

C# - 62

System.Runtime.InteropServices.Marshal.ReadInt32(IntPtr.Zero);

Edit: 23

unsafe{int i=*(int*)0;}

Must compile with /unsafe for this one to work. For some reason I don't understand, *(int*)0=0 just throws a NullReferenceException, while this version gives the proper access violation.

share|improve this answer
    
The int i=*(int*)0; returns a NullReferenceException for me. –  Peter Olson Dec 30 '11 at 7:43
    
You can try to access a negative location, like *(int*)-1=0 and get an access violation. –  Peter Olson Dec 30 '11 at 7:46
    
The particular exception is just what the clr wraps it in, and is insignificant. The os itself actually gives the seg fault in all these cases. –  CMP Jan 20 '12 at 17:41

Python, 33 characters

>>> import ctypes;ctypes.string_at(0)
Segmentation fault

Source: http://bugs.python.org/issue1215#msg143236

Python, 60 characters

>>> import sys;sys.setrecursionlimit(1<<30);f=lambda f:f(f);f(f)
Segmentation fault

Source: http://svn.python.org/view/python/trunk/Lib/test/crashers/recursive_call.py?view=markup

This is the Python version I'm testing on:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

In general the Python interpreter is hard to crash, but the above is selective abusiveness...

share|improve this answer

dc - 7 chars

[dx0]dx

causes a stack overflow

share|improve this answer

F90 - 25 chars

REAL A(1)
n=2
A(n)=0
END

Compiled with Intel compiler 12.0 which actually refuses to compile without the intermediate n as it detects the bound error. Does this count as a segfault?:

ifort -C sigsegv.F90 
./a.out 
forrtl: severe (408): fort: (2): Subscript #1 of the array A has value 2 which is greater than the upper bound of 1
share|improve this answer

Cython, 14

This often comes in handy for debugging purposes.

a=(<int*>0)[0]
share|improve this answer

PicoLisp - 4 characters

$ pil
: ('0)
Segmentation fault

This is intended behaviour. As described on their website:

If some programming languages claim to be the "Swiss Army Knife of Programming", then PicoLisp may well be called the "Scalpel of Programming": Sharp, accurate, small and lightweight, but also dangerous in the hand of the inexperienced.

share|improve this answer

brainfuck (2)

<.

Yes, this is implementation-dependent. SIGSEGV is the likely result from a good compiler.

share|improve this answer
    
How is a compiler that segfaults on that "good"? That < should either have no effect or wrap around. –  nyuszika7h Jul 4 at 17:38

J (6)

memf 1

memf means free memory, 1 is interpreted as a pointer.

share|improve this answer

Tcl

set a a;while {[incr i]<999999999} {set a [list $a]};puts $a

Not short, but crashes with a segfault.

share|improve this answer

Ti-Basic 84, 5 characters

prgmA

Name the program prgmA

P.S. ERR:MEMORY is roughly equivalent to a SIGSEGV

share|improve this answer

TI-BASIC

":While 1:Ans+"+:End

Eventually runs out of memory (ERR:MEMORY = SIGSEGV)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.