Shortest code that raises a SIGSEGV

  • Write the shortest code that raises a Segmentation Fault (SIGSEGV) in any programming language.

    Wow. Possibly the shortest successful question.

    @MatthewRoh Out of interest, I made this SEDE query. It looks like there are a few with +10 or higher, but this is the first above +40

  • C, 5 characters


    It's a 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.

    Try it online!

    Default value may not be 0, and this may give a BUS error rather than a segmentation fault.

    @Macmade: Actually, it is `0`. `static` variables start as `0`, and `main;` is `static`, as I declared it outside function.

    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.

    Yep, segfaults in C are pretty much the default :P

    `main __attribute__((section(".text#")))=0xc3;` FTFY (at least it seems to return without crashing on my x86).

    @jozxyqk Or shorter, `const main=195;`. As interesting it is that it's working, the goal of this code golfing challenge was to make the code segfault, not work :).

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM