Shortest code that return SIGSEGV among the given languages

7

0

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

Arya

Posted 2011-12-24T21:45:08.793

Reputation: 991

Question was closed 2017-01-04T14:12:08.137

6int main() { std::cout << "SIGSEGV(Segmentation fault )" << std::endl; } – sehe – 2011-12-24T22:00:30.773

but this is not shorter than above code!! – Arya – 2011-12-24T22:04:36.577

5movl $0,0 (GNU AS syntax, x86_64 - the modern assembly segfault) – None – 2011-12-24T22:04:53.650

3The 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 – 2011-12-24T22:05:02.077

Hilarious, @sehe! – None – 2011-12-24T22:06:43.860

14 characters: main(){*""=0;} – None – 2011-12-24T22:09:14.923

@DietrichEpp, I'm afraid compiler may object without casting off const. – None – 2011-12-24T22:21:35.567

2@sehe: That'd be both faster and shorter if you would do std::cout << "SIGSEGV(Segmentation fault )\n"; instead. :-) – None – 2011-12-24T22:23:41.190

@MichaelKrelin-hacker: Not in traditional C mode, with warnings turned off. – None – 2011-12-24T23:23:56.427

2This 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. – None – 2011-12-24T23:49:19.933

I'd be pretty interested in ANY java (excluding JNI/A since that'd be C again) code that produces a sigsev ;) – None – 2011-12-25T00:07:37.730

This is a "make a list" question, and that is "not constructive" writ large. – dmckee --- ex-moderator kitten – 2011-12-26T00:15:29.310

@dmckee There's also an "explain it" component, which is perhaps somewhat redeeming. – luser droog – 2012-10-21T17:44:52.290

The folks at nntp:comp.lang.c would have everyone know that using any declaration for main other that int main(void), void main(void), or int main(int,char**) is "implementation-defined", and therefore no longer standard C. – luser droog – 2012-10-21T17:47:55.080

Answers

18

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;

matja

Posted 2011-12-24T21:45:08.793

Reputation: 366

3That's pretty darn short. +1 And it compiles (though with a warning) – sehe – 2011-12-24T22:19:57.143

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. – None – 2011-12-24T22:28:45.137

6

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.

Dmitri

Posted 2011-12-24T21:45:08.793

Reputation:

6

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();}

Neo Adonis

Posted 2011-12-24T21:45:08.793

Reputation:

is there any shorter code than this in other language..? – Arya – 2011-12-24T21:58:38.973

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... – None – 2011-12-24T22:00:53.443

3

Assembly, 3 characters

RET

C, 5 characters

main;

Rontogiannis Aristofanis

Posted 2011-12-24T21:45:08.793

Reputation: 31

3

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

main(){++*((int*)"");}

Michael Krelin - hacker

Posted 2011-12-24T21:45:08.793

Reputation: 131

2

Ruby, 18 characters

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

Process.kill 11,$$

user4740

Posted 2011-12-24T21:45:08.793

Reputation:

2

Nah, as short as this :

int main;

trubilo

Posted 2011-12-24T21:45:08.793

Reputation: 21

3As the accepted answer shows, you don't even need the int. – luser droog – 2012-10-21T17:34:29.497