Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

recursive main()

LorienLorien Posts: 90Member
in a recent post, someone said something about c++ now allowing main() to be recursive. could someone tell me if this is true and maybe why?

also, even if it can't be rescursive, is it allowed that some other function makes a call to main()?
«1

Comments

  • Eric TetzEric Tetz Posts: 2,141Member
    : in a recent post, someone said something about c++ now allowing main() to be recursive. could someone tell me if this is true and maybe why?

    Recursive calls to main are not allowed (the standard is very clear on this point).

    : also, even if it can't be rescursive, is it allowed that some other function makes a call to main()?

    That's recursion, too (indirect), so it's not allowed.

    Cheers,
    Eric
  • LorienLorien Posts: 90Member
    could you be more specific as to what "not allowed" means? will a recursive main() not compile? will issue a warning? are wierd things likely to happen?
  • Eric TetzEric Tetz Posts: 2,141Member
    : could you be more specific as to what "not allowed" means?

    Well, the standard states:

    [b]3.6.1.3[/b]
    "The function main shall not be used within a program."

    [b]5.2.2.9[/b]
    "Recursive calls are permitted, except to the function named main"

    : will a recursive main() not compile? will issue a warning? are wierd things likely to happen?

    That all depends on your compiler. Once you've strayed outside the boundaries of the language defined by the standard, anything can happen. You're in the land of 'undefined behavior'. Of course, the behavior [italic]could[/italic] be defined for your particular compiler - you'll have to check the docs... but you can't say what it will do on another compiler. It could fail to compile, or crash at runtime, or it could send an email titled "I'm an idiot" to all your friends and relatives... you don't really know.

    Cheers,
    Eric
  • otsethotseth Posts: 9Member
    : : in a recent post, someone said something about c++ now allowing main() to be recursive. could someone tell me if this is true and maybe why?
    :
    : Recursive calls to main are not allowed (the standard is very clear on this point).
    :
    : : also, even if it can't be rescursive, is it allowed that some other function makes a call to main()?
    :
    : That's recursion, too (indirect), so it's not allowed.
    :
    : Cheers,
    : Eric
    :

    in BC3.1 when you call main directly you'll recive an error,
    but if you assign main to a pointer to function and call that pointer
    (i mean "int (*fn)()=main; fn();") then it will work
    i don't tested it with other compilers, but it should work with them
  • Eric TetzEric Tetz Posts: 2,141Member
    : in BC3.1 when you call main directly you'll recive an error,
    : but if you assign main to a pointer to function and call that pointer
    : (i mean "int (*fn)()=main; fn();") then it will work
    : i don't tested it with other compilers, but it should work with them

    I'm pretty sure it's legal in C, but I didn't check (don't care much about C). In any case, no one is saying that it won't [italic]work[/italic] on some compilers - but in C++ it's not legal. You compiler may accept plenty of illegal code and do [italic]something[/italic] with it. It's up to you whether you want to program in C++, or in whatever your compiler happens to accept.

    Cheers,
    Eric
  • compuchipcompuchip Posts: 273Member
    Anyway, why you want to know?
    You should avoid using recursive functions in the first place if possible.
    And calling main() also seems like a very bad idea to me.
    I don't know what you're trying to do, but I wouldn't do this until you're sure there is absolutely no other way you can achieve what you want.

    : : in BC3.1 when you call main directly you'll recive an error,
    : : but if you assign main to a pointer to function and call that pointer
    : : (i mean "int (*fn)()=main; fn();") then it will work
    : : i don't tested it with other compilers, but it should work with them
    :
    : I'm pretty sure it's legal in C, but I didn't check (don't care much about C). In any case, no one is saying that it won't [italic]work[/italic] on some compilers - but in C++ it's not legal. You compiler may accept plenty of illegal code and do [italic]something[/italic] with it. It's up to you whether you want to program in C++, or in whatever your compiler happens to accept.
    :
    : Cheers,
    : Eric
    :

  • LorienLorien Posts: 90Member
    : Anyway, why you want to know?
    : You should avoid using recursive functions in the first place if possible.

    why? the extra time it takes to call functions and push variables onto the stack isn't who knows what, and recursive functions can be written elegantly, so that they don't fill up the stack under normal conditions (there are languages in which the only way to achieve repetition is through recursion). besides, some people (me included), think and understand recursive implementations of algorithms far better than iterated ones.

    : And calling main() also seems like a very bad idea to me.

    i can see a few reasons why, in some programs, it might be useful to have main() be recursive. one of them would be that it's an easy way to make a program do the main task for more than one data-set. in this case you'd have something like:

    [code]
    main()
    {
    // ... do what you have to do
    if(!inputfile.eof()) return main();
    else return 0;
    }
    [/code]

    in some cases, this will be easier to read and comprehend than using a loop.

    : I don't know what you're trying to do, but I wouldn't do this until you're sure there is absolutely no other way you can achieve what you want.

    i write recursive functions every opportunity i get. =) i believe it's rather a matter of taste than efficiency.
  • Eric TetzEric Tetz Posts: 2,141Member
    : : And calling main() also seems like a very bad idea to me.
    :
    : i can see a few reasons why

    The fact that the standard forbids it is not good enough for you??!!! That's a damn good reason, as far as I'm concerned.

    : , in some programs, it might be useful to have main() be recursive. one of them would be that it's an easy way to make a program do the main task for more than one data-set. in this case you'd have something like:
    :
    : [code]
    : main()
    : {
    : // ... do what you have to do
    : if(!inputfile.eof()) return main();
    : else return 0;
    : }
    : [/code]

    This in no way requires you to call main recursively. Just copy all of the code in main into a seperate function, and let [italic]that[/italic] function call itself recursively. You end up doing the exact same thing, but with legal code:
    [code=ffffff]
    [color=000080]int[/color] dostuff()
    {
    [color=80a0b0][italic]// ... do what you have to do[/italic][/color]
    [color=000000][b]if[/b][/color](!inputfile.eof()) [color=000000][b]return[/b][/color] dostuff();
    [color=000000][b]else[/b][/color] [color=000000][b]return[/b][/color] [color=bb0000]0[/color];
    }

    [color=000080]int[/color] main() {
    [color=000000][b]return[/b][/color] dostuff();
    }
    [/code]
    Cheers,
    Eric

  • CytCyt Posts: 557Member
    : [b]3.6.1.3[/b]
    : "The function main shall not be used within a program."

    Now this is a bit... hmm... Interesting.

    I once made a layer above main in a windows program. This layer called main, which is of course not recursive, since there is no main in a windows program.

    So, was that "illegal", or is it sort of off-topic?
  • thefamousblurtthefamousblurt Posts: 3Member
    please delete this post - script filter said that 'submit' did not work.
«1
Sign In or Register to comment.