# Shortest code that return SIGSEGV

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

-

# 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 follow null pointer and causes SIGSEGV.

-

kill -11 $$ - +1 for thinking different. – FUZxxl Dec 26 '11 at 22:19 # 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. - ## C, 18 main(){raise(11);}  - ## 3 Characters in Assembly RET  writing only this will give Segmentation Fault. - 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 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 @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 show 1 more comment ## 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. - ## 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  - 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 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 show 4 more comments ## 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.

-

## 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.

-
 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

## 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.

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

## Python, 33 characters

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


## Python, 60 characters

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


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...

-

# Python 33

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


Sending signal 11 (SIGSEGV) in python.

-

# 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.

-

# dc - 7 chars

[dx0]dx


causes a stack overflow

-

## Cython, 14

This often comes in handy for debugging purposes.

a=(<int*>0)[0]

-

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

-