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.

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

share|improve this question

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

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

6  
int main() { std::cout << "SIGSEGV(Segmentation fault )" << std::endl; } –  sehe Dec 24 '11 at 22:00
5  
movl $0,0 (GNU AS syntax, x86_64 - the modern assembly segfault) –  Mat Dec 24 '11 at 22:04
3  
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. –  David Schwartz Dec 24 '11 at 22:05
2  
@sehe: That'd be both faster and shorter if you would do std::cout << "SIGSEGV(Segmentation fault )\n"; instead. :-) –  Omnifarious Dec 24 '11 at 22:23
2  
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. –  Omnifarious Dec 24 '11 at 23:49
show 9 more comments

7 Answers 7

up vote 17 down vote accepted

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;
share|improve this answer
3  
That's pretty darn short. +1 And it compiles (though with a warning) –  sehe Dec 24 '11 at 22:19
1  
+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. –  Omnifarious Dec 24 '11 at 22:28
    
+1, eloquently coded. –  Michael Krelin - hacker Dec 25 '11 at 17:55
add comment

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();}
share|improve this answer
    
is there any shorter code than this in other language..? –  Arya Dec 24 '11 at 21:58
    
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... –  Neo Adonis Dec 24 '11 at 22:00
add comment

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.

share|improve this answer
add comment

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

main(){++*((int*)"");}
share|improve this answer
add comment

Nah, as short as this :

int main;
share|improve this answer
3  
As the accepted answer shows, you don't even need the int . –  luser droog Oct 21 '12 at 17:34
add comment

Assembly, 3 characters

RET

C, 5 characters

main;
share|improve this answer
add comment

Ruby, 18 characters

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

Process.kill 11,$$
share|improve this answer
add comment

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.