2

It explicitly says in the c++ standard documentation that a program may not call main. Yet I wrote a program that calls main and works perfectly fine, why is that? The code:


#include<iostream>

static int counter = 0;

int main(){
    counter++;
    std::cout << counter << " It works" << std::endl;
    
    while(counter < 10){
        main();
    }

    return 1;
}

It prints to console "It works" 10 times. According to the standard documentation, this should not work, yet it works. What's going on?

LoopGod
  • 67
  • 5
  • 6
    undefined behavior means whatever you get is "correct". In C++, if you break the rules, it doesn't necessarily mean the program wont compile. – NathanOliver Nov 06 '21 at 16:37
  • Do you have a link to the documentation? I believe this is just undefined behavior. – Orace Nov 06 '21 at 16:38
  • @Orace https://en.cppreference.com/w/cpp/language/main_function what exactly does undefined behavior mean? – LoopGod Nov 06 '21 at 16:40
  • Think this through if the program would really have worked you would have seen a whole lot more "It works"... and then a stackoverflow (infinite recursion). So does it really work? (The program really shows undefined behavior) – Pepijn Kramer Nov 06 '21 at 16:40
  • See also this question: https://stackoverflow.com/questions/4144030/why-calling-main-is-not-allowed-in-c – Marc Stevens Nov 06 '21 at 16:41
  • 2
    Undefined behavior means that really anything could happen, including seemingly working fine. It might also cause crashes, [summon nasal demons](http://www.catb.org/jargon/html/N/nasal-demons.html), or cause your pets to spontaneously combust. Also see e.g. [How undefined is undefined behavior?](https://stackoverflow.com/questions/7961067/how-undefined-is-undefined-behavior) – Some programmer dude Nov 06 '21 at 16:44
  • While this is UB, "shall not" in the standard typically means that you'll get a diagnostic of some kind, which doesn't happen here. – cigien Nov 06 '21 at 17:17
  • 1
    @TedLyngmo Ah, I see. That clarification might be worth adding to your answer then, especially since the question explicitly mentions that they expect a diagnostic. – cigien Nov 06 '21 at 17:30
  • I get compilation errors with clang and gcc - live - https://godbolt.org/z/3dzf9vTGM – Richard Critten Nov 06 '21 at 17:30
  • Undefined behavior means the programmer messed up, and did something in violation to C++ standard. C++ is not a nanny language, and gives you enough rope to shoot yourself in the foot. – Eljay Nov 06 '21 at 18:39

1 Answers1

5

basic.start.main/3: The function main shall not be used within a program.

Violating this rule makes your program have undefined behavior - which means that the program can do pretty much anything. It may even do what you wanted it to do or appear to do what you wanted but have devastating side effects, so avoid having undefined behavior in your programs.

Regarding the lack of diagnostic messages: I suspect that some compilers, like g++, actually support calling main as an extension. I had to turn on -pedantic or -pedantic-errors to get the diagnostic message "ISO C++ forbids taking address of function '::main' [-Wpedantic]"

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108