4

Possible Duplicate:
Is it legal to recurse into main() in C++?

#include <iostream>
using namespace std;

int main() {
  static int var = 5;
  std::cout << --var;
  if(var)
    main();
}

gcc compiles the code http://ideone.com/lIp3A . I know that main cannot be used inside main in C++. How come this code compiles?

Community
  • 1
  • 1
PaulNathan
  • 51
  • 3
  • As far as I have learned, main -like any other fuction- can be called inside itself. – Tamer Shlash Mar 03 '11 at 05:59
  • Yup, just call it. Its just an entry point hooked by the runtime. Nothing special about it, other than the name. – Ritch Melton Mar 03 '11 at 06:00
  • 4
    @Mr. Tamer, @Ritch Melton: In C you're free to call `main`. In C++, you're not. The idea is that calling main might have hidden effects like calling static constructors that would cause problems if repeated. – Jerry Coffin Mar 03 '11 at 06:14
  • Please search before asking questions. – leppie Mar 03 '11 at 06:16
  • 1
    @leppie : I know that main cannot be used inside main. My question was why don't I get any error on gcc? – PaulNathan Mar 03 '11 at 06:18
  • 3
    @Ritch: wrong. Prasoon's answer covers the no-recursion rule; in addition, `main` is special in that it returns 0 if no `return` clause is specified. – Fred Foo Mar 03 '11 at 06:18
  • @larsmans Good point. I always just saw it as a regular function expected by the runtime. Lesson learned. – Ritch Melton Mar 03 '11 at 06:24
  • @Jerry Coffin I'd expect _main (or whatever internal main is called) do do things like BSS initialization, and call static constructors. I wouldn't expect my implementation of main to be modified to do that. But I've been incredibly wrong up until this point. – Ritch Melton Mar 03 '11 at 06:25
  • @Ritch, in many *implementations*, all the magic setup and glue to the OS is done by the true entry point which eventually calls main(), does all the magic teardown and winds up the process. But the standard allows the implementation to effectively inject that into main() itself. The versions of GCC I've used do treat `main` just like any other function but that is just an implementation detail. – RBerteig Mar 03 '11 at 06:57
  • @RBerteig- The various embedded C compliers (and linkers [if it was needed]) treated it like a normal function with a magic name. that's why I assumed (incorrectly) that it wasn't a big deal. – Ritch Melton Mar 03 '11 at 07:00
  • @Rich: There's plenty unusual about `main`. It must return `int`, it has explicit `return 0` if omitted, it may not be marked `inline` or `static`, it must be defined precisely once across all TUs, you may not take the address of it, you may not call it ..... – Lightness Races in Orbit Aug 11 '11 at 16:21

1 Answers1

17

The code is ill-formed because it violates the shall construct of §3.6.1.3

§3.6.1.3 says :

The function main shall not be used within a program.


The shall construct

A diagnosable rule is defined as (§1.4.1):

The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except for those rules containing an explicit notation that “no diagnostic is required” or which are described as resulting in “undefined behavior.”

§3.6.1.3 defines a diagnosable rule.

According to §1.4.2:

— If a program contains no violations of the rules in this International Standard, a conforming implementation shall, within its resource limits, accept and correctly execute that program.

— If a program contains a violation of any diagnosable rule, a conforming implementation shall issue at least one diagnostic message, except that

— If a program contains a violation of a rule for which no diagnostic is required, this International Standard places no requirement on implementations with respect to that program.


Conclusion

A compiler is free to do whatever it wants to do. Try the same code on Comeau Online (a more conforming compiler). I get this error "function "main" may not be called or have its address taken"

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 1
    @PaulNathan: That's not how I read it. GCC is still correct because of the third clause in 1.4.2 above. It does not need to generate the diagnostic message if no diagnostic is required. This indicates that calling main() works in GCC. Of course that does not mean it is portable. – Martin York Mar 03 '11 at 07:12
  • 3
    @Martin: that's not how I read it ;-). That third clause only applies to "rule[s] for which no diagnostic is required", and as per the quote from 1.4.1, such rules must be explicitly notated. No such notation appears in the vicinity of 3.6.1.3. – Tony Delroy Mar 03 '11 at 07:38
  • @Martin: It doesn't even mean that it "works" in GCC. _Anything_ may happen. – Lightness Races in Orbit Aug 08 '11 at 12:30
  • @Martin: But this is not a rule "for which no diagnostic is required". GCC should produce a diagnostic here. – Lightness Races in Orbit Aug 11 '11 at 16:27
  • 1
    @Prasoon: Your conclusion is wrong. This is not UB; it's an ill-formed program and the compiler can't "do whatever it wants to do"; it's supposed to emit a diagnostic. – Lightness Races in Orbit Aug 11 '11 at 16:28