Created
April 1, 2011 19:27
-
-
Save kbob/898702 to your computer and use it in GitHub Desktop.
Test whether program compiled with tail call optimization.
This file contains hidden or 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> | |
/* | |
* 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