Skip to content

Instantly share code, notes, and snippets.

@mqp
Created April 1, 2011 23:24
Show Gist options
  • Save mqp/899047 to your computer and use it in GitHub Desktop.
Save mqp/899047 to your computer and use it in GitHub Desktop.
TCO subtleties with C compilers
#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