2009-04-22

tail recursion / empty loop / overflow / -O2 / const / halting problem?

為瞭解 "CALL" 機制而不小心寫出來的程式.

$ cat tail.c
#include <stdio.h>
int f(int n)
{
return n == 1000001 ? 0xBAD : f(n + 2);
}
int main()
{
printf("%x\n", f(0));
return 0;
}
$ gcc tail.c
$ ./a.out
Segmentation fault
$
$ gcc -O2 tail.c
$ ./a.out
bad
$

1 comment:

Anonymous said...

Nice dispatch and this post helped me alot in my college assignement. Thank you seeking your information.