C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28645
Number of posts: 94661

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
recursive main() Posted by Lorien on 5 Apr 2002 at 1:00 PM
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()?
Report
Re: recursive main() Posted by Eric Tetz on 5 Apr 2002 at 1:20 PM
: 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
Report
Re: recursive main() Posted by Lorien on 5 Apr 2002 at 1:35 PM
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?
Report
Re: recursive main() Posted by Eric Tetz on 5 Apr 2002 at 1:55 PM
: could you be more specific as to what "not allowed" means?

Well, the standard states:

3.6.1.3
"The function main shall not be used within a program."

5.2.2.9
"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 could 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
Report
Re: recursive main() Posted by Cyt on 8 Apr 2002 at 5:54 AM
: 3.6.1.3
: "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?
Report
Re: recursive main() Posted by otseth on 6 Apr 2002 at 2:17 PM
: : 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
Report
Re: recursive main() Posted by Eric Tetz on 6 Apr 2002 at 3:25 PM
: 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 work on some compilers - but in C++ it's not legal. You compiler may accept plenty of illegal code and do something with it. It's up to you whether you want to program in C++, or in whatever your compiler happens to accept.

Cheers,
Eric
Report
Re: recursive main() Posted by compuchip on 7 Apr 2002 at 11:01 AM
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 work on some compilers - but in C++ it's not legal. You compiler may accept plenty of illegal code and do something with it. It's up to you whether you want to program in C++, or in whatever your compiler happens to accept.
:
: Cheers,
: Eric
:

Report
Re: recursive main() Posted by Lorien on 7 Apr 2002 at 2:17 PM
: 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:

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


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.
Report
Re: recursive main() Posted by Eric Tetz on 7 Apr 2002 at 2:38 PM
: : 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:
:
:
: main()
: {
:   // ... do what you have to do
:   if(!inputfile.eof()) return main();
:   else return 0;
: }
: 


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

int main() {
  return dostuff();
}

Cheers,
Eric

Report
Re: recursive main() Posted by thefamousblurt on 21 Jun 2012 at 10:44 PM
please delete this post - script filter said that 'submit' did not work.
Report
Re: recursive main() Posted by thefamousblurt on 21 Jun 2012 at 10:46 PM
please delete this post - script filter said that 'submit' did not work.
Report
Re: recursive main() Posted by thefamousblurt on 21 Jun 2012 at 10:47 PM
Hi, I did this, for a self-documenting & running program which runs its own example:
#include <stdio.h>
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!" };

	<print out help information>

	// 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[])
{
	<this is your mainly main program! >

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

	<go on with your normal processing>
}
This results in:
$ 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 ~
$



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.