recursive main()

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()?

Comments

  • : 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
  • 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?
  • : 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
  • : : 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
  • : 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
  • 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
    :

  • : 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.
  • : : 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

  • : [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?
  • please delete this post - script filter said that 'submit' did not work.
  • please delete this post - script filter said that 'submit' did not work.
  • Hi, I did this, for a self-documenting [b]& running[/b] program which runs its own example:
    [code]
    #include
    etc, etc, etc

    #define CAPACITY_OF(array) (sizeof(array)/sizeof(array[0]))

    /* Prototype for mainly() function */
    int mainly(int argc, char *argv[]); /* Recursively calling main not allowed. */

    /**********************************************/
    void usage(FILE *file)
    {
    char *example[] = {
    "lgchr", "-w", "79", "-b", "3", "-C", "-s", "c", "-n", "Doh!" };



    // Now run the program using the example:
    mainly(CAPACITY_OF(example), example);
    }
    /**********************************************/
    int main(int argc, char *argv[])
    {
    return mainly(argc, argv);
    }
    /**********************************************/
    int mainly(int argc, char *argv[])
    {


    if < an option is '-h' >
    {
    usage(stdout);
    exit(1);
    }


    }[/code]This results in:[code]$ lgchr -h
    lgchr 5.1 (c) 1991-2012 Alf Lacis Build:20120622.151856

    lgchr: create headers in various sizes to include in source code, text files, etc.

    Usage:
    lgchr [-s style] [-b n] [-t value|a|i] [-c [c|r|b|d]] [-w minw] [-C] [-n] [-f file]|[string1 [string2...]] >out

    Where:
    STYLE CASE ASCII FIXED/PROP BLANKS
    -s style ===== =========== ========= ============ ======
    a 16x12 UPPER+lower 32 -> 127 proportional 1
    b 12x12 UPPER ONLY 32 -> 95 proportional 1
    c 12x7 UPPER+lower 32 -> 127 proportional 2
    d 9x5 UPPER+lower(*) 32 -> 127 proportional 1
    e 7x5 UPPER ONLY 32 -> 95 proportional 1
    f 7x4 UPPER+special(#) 32 -> 96 fixed fixed

    (*) Default style is d = 9x5.
    (#) Non-ASCII characters are declared in 97 to 127.

    -b n Number of blanks between letters

    -t a means use the character to make the large letter (default=@)
    i means use the IBM square block character (decimal 219)
    value a value in decimal for the character to be displayed

    -c c Comments: C format: "/*..." Default is C++: "//..."
    -c r raw format: just the text, not a C or C++ header
    -c b bash format: use '#' for the comment start (also perl, etc)
    -c d dos format: use 'REM ' for the comment start

    -w minw Minimum width of top & bottom 'lines', default is 100

    -C Centre the heading within the minimum width: also see -w

    -n No empty lines above & below text

    -f file Read rows of strings from this file

    string1, string2, etc - strings to be converted

    Note: Maximum output line length as at Jun 22 2012 is 20480.

    Example:
    lgchr -w 79 -b 3 -C -s c -n Doh!
    ///////////////////////////////////////////////////////////////////////////////
    @
    // @ @
    // @ @
    // @
    // @ @ @ @ @
    // @ @ @ @ @
    // @ @ @ @ @
    // @ @ @ @
    @ @
    ///////////////////////////////////////////////////////////////////////////////
    homer@AU-L4A102325HL ~
    $[/code]
Sign In or Register to comment.

Howdy, Stranger!

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

Categories