main calling main

juglers's Avatar, Join Date: Aug 2010
Newbie Member
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?
0
Ancient Dragon's Avatar, Join Date: Jul 2010
Go4Expert Member
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.
shabbir like this
0
juglers's Avatar, Join Date: Aug 2010
Newbie Member
thanks for the reply
but a question again,
is the stack size predefined or is it defined by the compiler ?
0
Ancient Dragon's Avatar, Join Date: Jul 2010
Go4Expert Member
Read this article
shabbir like this
0
LordN3mrod's Avatar, Join Date: Sep 2010
Go4Expert Member
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