Why is it wrong to call the main function directly?

Someone has told me that calling the main function directly is not legal.
I have done it before and it makes my program run exactly as I want it to.
Thanks for any explanation.
closed account (S6k9GNh0)
I'm guessing its because main isn't actually you're starting function. In Linux, it's _init for dynamic libraries and _start for everything else. main is just an abstraction. So, perhaps its not ethical to assume that the compiler keeps main as main internally.

Just so you know, this is allowed in C. g++ (GCC C++) also allows this if you tell it to shut up.

In practice, you can do it but it's not smart.
so basically its the law and we don't need to do that. Even if it works fine for what we are doing.
closed account (S6k9GNh0)
Just because something "works fine" doesn't mean it's the most correct or clean. It can cause problems in the long run so just avoid it is the philosophy here.
For one thing, if you use a different compiler, the code may not even compile. It's not useful to get into the habit of using non-standard code which is compiler-specific. It means you bypass the process of learning how to do things correctly.
^ That.
Once main() have called itself many times, it will crash for stack overflow.
Stack memory isn't so big.

Stick with do/while.

Let me write you the reason:

Say you have like 32 bytes of stack memory, just as an example:

00000000000000000000000000000000


A '0' means an unused byte. An '1' means an used byte.

You enter main, the function call requires two 32-bit parameters:
11111111000000000000000000000000


You want to store a variable.
Say we store two 32-bit integers.
11111111111111110000000000000000


You do another call to main(), so it's 32-bit of parameters and another 32-bits of variables.

11111111111111111111111111111111


You can see how, after some time, the stack fills itself.
The stack isn't this little, but you should use a good use of it.

Using do/while, you will reuse the same stack over and over again.
Last edited on
ok the reason I asked was I did it on a resent program i was practicing with I fixed it by just making my main another function then writing main to just call to my other function then i never have to refer to main again is that alright?
If your other function then calls itself that could be legal, but not necessarily good practice.
glrader wrote:
ok the reason I asked was I did it on a resent program i was practicing with I fixed it by just making my main another function then writing main to just call to my other function then i never have to refer to main again is that alright?


You should not make recursion over the same function over and over again.

Take this as an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

int myprogram(int, char**);

int main(int argc, char** argv)
{
    int result = 1;
    while(1)
    {
        result = myprogram(argc,argv);
        if(result == 0)
            return 0;
        else if(result < 0)
            return 1;
    }
    return 0;
}

int myprogram(int argc, char** argv)
{
    // There goes your program.
    // To make it run again, return 1.
    // To make it quit successfully, return 0.
    // To make it quit telling Windows/Linux/Whatever you had an error, return -1.
}


You can use it as a code base.
Last edited on
ok thanks so my program doesn't have functions calling their selves so I guess Im just fine here is my program so you can see wht i was practicing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdlib>
using namespace std;
int randvar1,randvar2,answer1,userimput;
int go();
int intro();
int question();
int rng();
int main()
{return rng();}
int rng()
{srand(time(NULL));
    randvar1=(rand()%100)+1;
    randvar2=(rand()%100)+1;
    static unsigned int call_count=0;
    call_count++;
    if (call_count==1)
    {return intro();}
    else if (call_count>1)
    {
    return question();}
}
int intro()
{ 
	
	
	cout<< "welcome to my test how are you?"<< "\n" << "now for the first question";
	return question();}
	int question()
	{
		cout<<"\n" <<"what is " << randvar1 << "+" << randvar2 <<"?\n";
	answer1=randvar1+randvar2;
	cin >> userimput;
	if (answer1==userimput)
	{
		cout<< "good job\n";
}
else if (answer1 != userimput)
{
	cout<< "wrong\n";
	
		}
	return go();}
	int go()
	{
	char selection;
	cout << "continue? y for yes n for no. ";
	cin >> selection;
	if (selection=='y')
	{return rng();
	}
	else if  (selection=='n'){
    }return 0;
}
You didn't even need to call main() more times in the first place then?
Unless your question() function does.

Argh, I cannot watch that indentation, I better get some sleep. Night.
Thank you all for your Input it helped a lot!
The above code is recursive.

rng() calls intro()
intro() calls question()
question() calls go()

then
go() calls rng()

That is still a function calling itself, but in this case indirectly.
Last edited on
Topic archived. No new replies allowed.