I tried this and was expecting it to recurse infinitely (until stack fault).
But it did not.
I used gcc.Code:int (*foo) (int, char**); int main(int argc, char** argv) { foo = main; foo(argc, argv); return 0; }
I tried this and was expecting it to recurse infinitely (until stack fault).
But it did not.
I used gcc.Code:int (*foo) (int, char**); int main(int argc, char** argv) { foo = main; foo(argc, argv); return 0; }
See this topic: http://cboard.cprogramming.com/showthread.php?t=55914
I believe calling main() recursively is allowed, however, due to complications of calling conventions or other such things under the hood, the behavior is undefined, although the standard makes no statement.
Oh so may be there are some problems related to recursion of the main method in C.
Not a problem I will try the above with a different function.
Thanks.
Damn!
Even this did not work
Code:int (*foo) (int, char**); int bar(int c, char** a) { foo = bar; foo(c, a); } int main(int argc, char** argv) { bar(argc, argv); return 0; }
Define "does not work"...
--
Mats
This works for me:
Yours compiles with the following errors:Code:#include <stdio.h> int mymain(int, char**); int mymain(int argc, char**argv) { printf("Entering mymain......\n"); mymain(argc, argv); return 0; } int main(int argc, char** argv) { mymain(argc, argv); return 0; }
If I fix that with a return to 0 and run it, it ends up finishing without recursing. If I add a printf() statement, then it ends up recursing properly. Perhaps it's recognizing and optomizing the endless recursion to never occur.recurmain.c: In function `bar':
recurmain.c:24: warning: no return statement in function returning non-void
recurmain.c:24: warning: control reaches end of non-void function
That's an invalid optimization, but I guess that it may decide that the call is just a call to itself, and since the function doesn't change anything, it will just skip the call anyways (a function that doesn't change anything, and that doesn't call anything else don't have to be called - since there's no way to detect the difference).
--
Mats
Yeah, I didn't expect GCC to do that, but I suppose it makes sense from their perspective. If nothing happens, especially endless recursion, why bother? lol.
> Even this did not fell into an infinite recursion!
Look up tail recursion elimination. Such cases are handled as a jump rather than a call, and as such do not cause stack overflow.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
As described by MacGyver, the problem is that the compiler believes that this function is a "no-operation" and eliminates the call to it. If you make the function "do something", such as increment argc, print something, return random() or something else that cause the function to actually have at least some side-effect, then you will be able to do it.
Or try with "no optimization" in the compiler, that should make the compiler not remove the call - verify by looking at the assembler output from the compiler.
--
Mats
matsp, i tried with gcc -O0 fptr.c
but this also did not help.
i am making an indirect recursion, through a function ptr, i thought it would work, but ...
As Salem said there no way the stack can grow in this case. Since there is no code below your call to mymain. Which in case is just a jump to the mymain. Since no data is copied on to the stack. It just a infinite loop forever. If you really wanted to see the stack grows exe this simple code
You get seg faultCode:#include <stdio.h> int foo() { foo(); printf("Let the stack grow\n"); } int main() { foo(); getchar(); return 0; }
ssharish2005
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"