This is a program that prints infinite numbers.
#define false 0
#define true 1
int test(int idx) {
printf("%d\n",idx);
test(idx+1);
return 0;
}
int main() {
test(0);
return 0;
}
// Segmentation fault: 11
This program ended with a segfault after printing 262045. I understand it is caused by stack overflow.
Is there any clever trick that can make the recursion go deeper?
Like calling another recursive function when it reaches a certain number and clears the stack?
I tried doing this.
#define false 0
#define true 1
int test2(int idx) {
printf("test2 here\n");
printf("%d\n",idx);
test2(idx+1);
return 0;
}
int test(int idx) {
if (idx == 262000) {
return test2(idx);
}
printf("%d\n",idx);
test(idx+1);
return 0;
}
int main() {
test(0);
return 0;
}
But the stack is not cleared. There is still a segfault after printing 262044.
I'm implementing regular expressions to match a file. I read and match the file using a recursive function, but if the file gets too big, there will be a segfault. I will be able to fix that if this question is solved.