main calling main

Discussion in 'C' started by juglers, Aug 7, 2010.

  1. juglers

    juglers New Member

    Joined:
    Aug 7, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. Ancient Dragon

    Ancient Dragon New Member

    Joined:
    Jul 23, 2010
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    part time cashier at WalMart
    Location:
    near St Louis, IL, USA
    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 likes this.
  3. juglers

    juglers New Member

    Joined:
    Aug 7, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    thanks for the reply
    but a question again,
    is the stack size predefined or is it defined by the compiler ?
     
  4. Ancient Dragon

    Ancient Dragon New Member

    Joined:
    Jul 23, 2010
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    part time cashier at WalMart
    Location:
    near St Louis, IL, USA
  5. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice