Created
April 1, 2011 23:24
-
-
Save mqp/899047 to your computer and use it in GitHub Desktop.
TCO subtleties with C compilers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
long acc_normal_rec(long n, long acc) | |
{ | |
if (n == 0) return acc; | |
long localacc = acc + 1; | |
return acc_normal_rec(n-1, localacc); | |
} | |
long acc_normal(long n) | |
{ | |
long acc = 0; | |
return acc_normal_rec(n, acc); | |
} | |
long acc_evil_rec(long n, long* acc) | |
{ | |
if (n == 0) return *acc; | |
long localacc = *acc + 1; | |
return acc_evil_rec(n-1, &localacc); | |
} | |
long acc_evil(long n) | |
{ | |
long acc = 0; | |
return acc_evil_rec(n, &acc); | |
} | |
int main(void) | |
{ | |
printf("%ld", acc_normal(1000)); | |
printf("%ld", acc_evil(1000)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment