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
6
int main() { std::cout << "SIGSEGV(Segmentation fault )" << std::endl; }
– sehe – 2011-12-24T22:00:30.773but this is not shorter than above code!! – Arya – 2011-12-24T22:04:36.577
5
movl $0,0
(GNU AS syntax, x86_64 - the modern assembly segfault) – None – 2011-12-24T22:04:53.6503The 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 thatint main(void)
,void main(void)
, orint main(int,char**)
is "implementation-defined", and therefore no longer standard C. – luser droog – 2012-10-21T17:47:55.080