Skip to content

Instantly share code, notes, and snippets.

@kbob
Created April 1, 2011 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbob/898702 to your computer and use it in GitHub Desktop.
Save kbob/898702 to your computer and use it in GitHub Desktop.
Test whether program compiled with tail call optimization.
#include <stdio.h>
/*
* In a separate compilation unit, save is defined as:
*
* void save(int *p)
* {
* *p = (int)&p;
* }
*/
extern void save(int *);
void recurse(int n, int *p)
{
if (n == 0)
return;
save(p);
recurse(n - 1, p + 1);
}
int is_tail_recursive(void)
{
int a[2];
recurse(2, a);
return a[0] == a[1];
}
int main(int argc, char *argv[])
{
if (is_tail_recursive()) {
printf("tail recursive\n");
while (1)
continue;
}
else
printf("not tail recursive\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment