+ 1
How to call main function in c++ from another function.
6 Respostas
+ 3
Call main(), why you need to do that anyway ?
+ 1
You can call main like any normal function.
But it most likely will cause infinite loop
main() call f() call maim() call f() ...
+ 1
The constructor/destructor pattern might work--you need the stack to pop--but calling main() from the destructor only works once:
https://code.sololearn.com/cCnk83IEL9Ff/?ref=app
[these C's should be portable to c++]
Here I use a fatal signal (a Floating Point Exception) to effectively GOTO. It's weird (why not just goto) but should be safe.
https://code.sololearn.com/c99KPGHr5FIb/?ref=app
...And here...I do call main repeatedly but...just for fun (don't do this); I'm jumping mid-instruction and falling through the 'constructor area' again; it leaks memory and should probably segfault (try removing the printf() to get Memory Limit Exceeded):
https://code.sololearn.com/cYgU9YGHlfD1/?ref=app
The point of including the last one is to emphasize: you have to pop the call stack (i.e. return or crash)
.
0
I am making a project on data handling.So I have to go to beginning of main function again and again
0
By the way, your clarification "go to the beginning again..." can be solved with this pattern:
int main() {
while(1==1) {
// ...stuff...
if(someTest==exitCondition) break;
}
return 0;
}
0
Randomzz - Lets Begin The Fun
Generally the patern would be this:
main()
{
init()
ret = main_loop() //real stuff happens here. It can be recursive or whatever.
deinit()
return ret
}