7
\$\begingroup\$

This question already has an answer here:

I come across a question in a coding competion

"write a code that returns SIGSEGV(Segmentation fault ) " .

Points were given on the basis of length of code. The prgramming languages available were asm,c,c++,java .

I knew c language better and submitted my code as

   main(){char *c="h";c[3]='j';}    

But others solve it in shorter length than me . How it is possible ? Can any one explain plz.

Platform UBUNTU 10.04

\$\endgroup\$

marked as duplicate by user62131, redstarcoder, Kritixi Lithos, clismique, Anthony Pham Jan 4 '17 at 14:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Jul 26 '12 at 2:22

This question came from our site for professional and enthusiast programmers.

  • 6
    \$\begingroup\$ int main() { std::cout << "SIGSEGV(Segmentation fault )" << std::endl; } \$\endgroup\$ – sehe Dec 24 '11 at 22:00
  • 5
    \$\begingroup\$ movl $0,0 (GNU AS syntax, x86_64 - the modern assembly segfault) \$\endgroup\$ – Mat Dec 24 '11 at 22:04
  • 3
    \$\begingroup\$ The question really can't be answered without reference to a specific platform. There is, for example, no C code that is guaranteed to give a SIGSEGV on every platform. \$\endgroup\$ – David Schwartz Dec 24 '11 at 22:05
  • 2
    \$\begingroup\$ @sehe: That'd be both faster and shorter if you would do std::cout << "SIGSEGV(Segmentation fault )\n"; instead. :-) \$\endgroup\$ – Omnifarious Dec 24 '11 at 22:23
  • 2
    \$\begingroup\$ This is a question that has an answer. There is a shortest program that produces the desired result. I don't agree that it should've been closed. \$\endgroup\$ – Omnifarious Dec 24 '11 at 23:49
18
\$\begingroup\$

bending the rules a bit ; the C linker doesn't care if 'main' is a function or not :

int main=0;

indeed, the default type is 'int' and the default global initialiser is 0 :

main;
\$\endgroup\$
  • 3
    \$\begingroup\$ That's pretty darn short. +1 And it compiles (though with a warning) \$\endgroup\$ – sehe Dec 24 '11 at 22:19
  • 1
    \$\begingroup\$ +1: Though, if it were my question, that would be bending the rules too far for me to accept as an answer. But that's still really impressive, and likely about the shortest you can go. \$\endgroup\$ – Omnifarious Dec 24 '11 at 22:28
6
\$\begingroup\$

There are many ways to make a program return SIGSEGV. One of these is via segmentation fault due to stack overflow of a recursive function calling itself a lot of times. In C, a short (if not the shortest) code for this would be:

main(){main();}
\$\endgroup\$
  • \$\begingroup\$ is there any shorter code than this in other language..? \$\endgroup\$ – Arya Dec 24 '11 at 21:58
  • \$\begingroup\$ In java, even the 'public static void main' is larger than this. Probably there's a shorter solution in asm, but I'm not really sure about it... \$\endgroup\$ – Neo Adonis Dec 24 '11 at 22:00
6
\$\begingroup\$

In C, something like this is fairly short:

int main(void)
{
  return *(int *)0=1;
}

...or for a less proper version with minimal whitespace:

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

...even less proper, but shorter (the source code, at least):

main(){puts(0);}

It calls puts() with an implicit prototype, but still "works" if linked with the standard library.

\$\endgroup\$
3
\$\begingroup\$

Besides may other ways of suicide as demonstrated by others, your way could be expressed a bit shorter as

main(){++*((int*)"");}
\$\endgroup\$
3
\$\begingroup\$

Assembly, 3 characters

RET

C, 5 characters

main;
\$\endgroup\$
2
\$\begingroup\$

Nah, as short as this :

int main;
\$\endgroup\$
  • 3
    \$\begingroup\$ As the accepted answer shows, you don't even need the int . \$\endgroup\$ – luser droog Oct 21 '12 at 17:34
2
\$\begingroup\$

Ruby, 18 characters

I know this wasn't requested, but have a Ruby one, because why not. :D

Process.kill 11,$$
\$\endgroup\$

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