Skip to content

Instantly share code, notes, and snippets.

@msg555
Last active September 10, 2024 12:44
Show Gist options
  • Save msg555/5418199 to your computer and use it in GitHub Desktop.
Save msg555/5418199 to your computer and use it in GitHub Desktop.

Revisions

  1. msg555 revised this gist Apr 19, 2013. 1 changed file with 14 additions and 11 deletions.
    25 changes: 14 additions & 11 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -5,20 +5,23 @@
    #define STACK_PAD 128

    int rec(int n) {
    if(n == 0) return 0;
    int r = rec(n - 1);
    ++r;
    return r;
    }

    void nmain() {
    printf("%d\n", rec(1000000));
    exit(0);
    return n == 0 ? 0 : rec(n - 1) + 1;
    }

    int main() {
    /* Allocate a new stack to use first. Because stacks grow to lower addresses
    * we want to use just a little before the end of the allocation. */
    char* nstack = (char*)malloc(STACK_SIZE) + STACK_SIZE - STACK_PAD;
    char lols[(char*)&nstack - nstack];
    nmain();

    /* Adjust the stack pointer to fall right in our newly allocated stack.
    * "char stack_hack[sz];" has the effect of subtracting sz from the stack
    * pointer. */
    char stack_hack[(char*)&nstack - nstack];

    /* Now we can recurse deeply. If you comment out the definition of stack_hack
    * then (on my system's limits, anyway) the second one will segfault. */
    printf("%d\n", rec(100000));
    printf("%d\n", rec(1000000));

    return 1;
    }
  2. msg555 revised this gist Apr 19, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <alloca.h>

    #define STACK_SIZE (1 << 26)
    #define STACK_PAD 128
    @@ -19,7 +18,7 @@ void nmain() {

    int main() {
    char* nstack = (char*)malloc(STACK_SIZE) + STACK_SIZE - STACK_PAD;
    alloca((char*)&nstack - nstack);
    char lols[(char*)&nstack - nstack];
    nmain();
    return 1;
    }
  3. msg555 created this gist Apr 19, 2013.
    25 changes: 25 additions & 0 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <alloca.h>

    #define STACK_SIZE (1 << 26)
    #define STACK_PAD 128

    int rec(int n) {
    if(n == 0) return 0;
    int r = rec(n - 1);
    ++r;
    return r;
    }

    void nmain() {
    printf("%d\n", rec(1000000));
    exit(0);
    }

    int main() {
    char* nstack = (char*)malloc(STACK_SIZE) + STACK_SIZE - STACK_PAD;
    alloca((char*)&nstack - nstack);
    nmain();
    return 1;
    }