Last active
August 29, 2015 14:09
-
-
Save benwaffle/91838ced9d8f014d4f22 to your computer and use it in GitHub Desktop.
print ascending numbers without my own state variables
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> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <libunwind.h> | |
void countForwardTo(int x) { | |
if (x > 1) | |
countForwardTo(x-1); // standard recursion | |
printf("%d", x); | |
// get the state of the stack | |
unw_context_t ctx; | |
unw_getcontext(&ctx); | |
// get a cursor to the previous stack frame | |
unw_cursor_t cursor; | |
unw_init_local(&cursor, &ctx); | |
unw_step(&cursor); | |
// get the name of the calling function | |
unw_word_t off; | |
char caller[100]; | |
unw_get_proc_name(&cursor, caller, 100, &off); | |
if (strcmp(caller, __func__) == 0) // if caller == this | |
printf(", "); | |
else // not a recursive stack frame | |
printf("\n"); | |
} | |
int main(int argc, char **argv) { | |
if (argc > 1) | |
countForwardTo(atoi(argv[1])); | |
} |
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
LDFLAGS=-lunwind -lunwind-x86_64 | |
all: asc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment