Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   main calling main (http://www.go4expert.com/forums/main-calling-main-t22960/)

juglers 7Aug2010 18:22

main calling main
i wanted to test whether main can call itself..it does
but how many times? mine ran 43336 times
what restricts the no. of calling of main?

Ancient Dragon 7Aug2010 18:33

Re: main calling main
What you did is called recursion. And the number of times a function can call itself is depended on the number of parameters to the function and the stack size of the program. Every time a function is called, whether recursive or not, the program has to save the values of important registers on the stack so that they can be restored when program control is returned back to the function, and it has to also save the value of all parameters on the stack (there are a few times when that is not done, but I'll ignore them now). So a function with no parameters can be called recursively more times than a function that has one or more parameters. The more parameters the fewer times it can be called.

juglers 7Aug2010 18:55

Re: main calling main
thanks for the reply
but a question again,
is the stack size predefined or is it defined by the compiler ?

Ancient Dragon 7Aug2010 19:08

Re: main calling main
Read this article

LordN3mrod 7Sep2010 01:18

Re: main calling main
In the current C++ standard it is clearly stated that the main function is special in that it cannot be overloader, called recursively and taken its address. Even if your implementation accepts such a program(which is ill-formed), that code is unsafe and not portable. Don't do that

All times are GMT +5.5. The time now is 07:34.