C] Question about tail recursion and stack, regsiter

Viewed 35

My question is this: "using global variables in recursion saves stack?". Surly It will reduce useless declarations. However, I wanna know is it also true that this method makes the function use only register not stack.

I mean, can't the below code work without any new stack?

// just binary search
int middle; // <- is this useless than i thought?
int binsearch(stdnt list[SOME_NUM], char target[SOME_NUM], int left, int right,  stdnt **found) {
    if (left <= right) {
        middle = (left + right) / 2;
        
        switch (strcmp(list[middle].name, target)) {
        case -1:
            return binsearch(list, target, middle + 1, right, found);
        case 0:
            *found = &list[middle]; return middle;
        case 1:
            return binsearch(list, target, left, middle - 1, found);
        }
        
    }

    return -1;
}
0 Answers
Related