7
\$\begingroup\$

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\$
14
  • 8
    \$\begingroup\$ int main() { std::cout << "SIGSEGV(Segmentation fault )" << std::endl; } \$\endgroup\$ Commented Dec 24, 2011 at 22:00
  • 6
    \$\begingroup\$ movl $0,0 (GNU AS syntax, x86_64 - the modern assembly segfault) \$\endgroup\$ Commented Dec 24, 2011 at 22:04
  • 4
    \$\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\$ Commented Dec 24, 2011 at 22:05
  • 3
    \$\begingroup\$ @sehe: That'd be both faster and shorter if you would do std::cout << "SIGSEGV(Segmentation fault )\n"; instead. :-) \$\endgroup\$ Commented Dec 24, 2011 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\$ Commented Dec 24, 2011 at 23:49

7 Answers 7

19
\$\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\$
2
  • 3
    \$\begingroup\$ That's pretty darn short. +1 And it compiles (though with a warning) \$\endgroup\$ Commented Dec 24, 2011 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\$ Commented Dec 24, 2011 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\$
2
  • \$\begingroup\$ is there any shorter code than this in other language..? \$\endgroup\$ Commented Dec 24, 2011 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\$ Commented Dec 24, 2011 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\$
1
  • 3
    \$\begingroup\$ As the accepted answer shows, you don't even need the int . \$\endgroup\$ Commented Oct 21, 2012 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\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.