How to close a program while not in main [duplicate] - c++

This question already has answers here:
How do I make a C++ console program exit?
(13 answers)
How can I immediately close a program in C?
(4 answers)
Closed 9 years ago.
Is there a line in c++ that allows me to terminate my program while I am not in main()?
For example, a code where if you die you have to quit the game? I don't want it to rely on the "trust" system.

Is there a line in c++ that allows me to terminate my program while I am not in main()?
I would not use the word "line" here - better say "function". For normal termination, you can use std::exit() (also see § 18.5/8 of the C++11 Standard):
#include <cstdlib>
void foo()
{
std::exit(EXIT_SUCCESS);
}
int main()
{
foo();
}
Here is a live example.

Related

Why this code does NOT give segmentation fault? [duplicate]

This question already has answers here:
Does "Undefined Behavior" really permit *anything* to happen? [duplicate]
(9 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 1 year ago.
This is a simple program I wrote to induce segfault
#include<iostream>
using namespace std;
char* func(){
char ch='a';
char *ptr=&ch;
return ptr;
}
int main()
{
char *ptr=func();
cout<<*ptr;
}
As variable ch must've been freed after func() scope ends, I thought that this program would give segmentation fault.
But it shows output as 'a'. What am i missing ?

What is the scope and evaluation of this if [duplicate]

This question already has answers here:
Declaring and initializing a variable in a Conditional or Control statement in C++
(9 answers)
Defining a variable in the condition part of an if-statement?
(5 answers)
Closed 1 year ago.
When cleaning up some code that I have found online, I came across with this weird c++ line:
if (int i = 1) std::cout << i;
With LLVM it compiled fine and the console output is 1, but how does the scope is handled in here, shouldn't the i variable be only accessible inside the conditional (inside the parenthesis)? And how is that possible to be evaluated to true, isn't an assignment a void operation and with no value, so 0/false? What is going on with this line?

A reason to forbid recursive call of main() in C++ [duplicate]

This question already has answers here:
Why calling main() is not allowed in C++
(3 answers)
Why are recursive main() calls not allowed in C++? [duplicate]
(2 answers)
Closed 2 years ago.
C++ standard says:
The function main shall not be used within a program.
Also:
Recursive calls are permitted, except to the main function.
At the same time C allows such a usage of main according to this answer. I found a comment under that answer that says the following:
The simplest implementation of global constructors (without special support from the OS and underlying C runtime entry code) is for the C++ compiler to generate a function call at the beginning of main (__main is a common name for it) which calls all the global constructors. Having global objects be reconstructed every time main gets called recursively would be a rather bad thing... :-)
It makes sense, but I've tried the following code:
#include <cstdio>
struct S {
S() { std::puts("S ctor"); }
~S() { std::puts("S dtor"); }
};
S s;
int main() {
static int count = 0;
count++;
if (count <= 10) {
std::printf("%d ", count);
return main();
}
std::puts("");
}
in clang, gcc, msvc. All of these compilers print same output:
S ctor
1 2 3 4 5 6 7 8 9 10
S dtor
So the global s object was contructed only once despite recursive call of main. Yes, I know what are undefined/unspecified/implementation-defined behaviour mean. But would someone explain (or even demonstrate by code for any available in internet compiler) in more details why using a main function in the C++ program is forbidden and may lead to unexpected results in particular?

Why does main() have to return an int? [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 9 years ago.
In most cases int main() does not return anything, it doesn't even have to since no return would not give an error. So why does main have to return an int? Why is void main not possible?
EDIT: I meant, why is int main() the standard if there usually is no return?
Other programs may use the return code to determine whether the application executed successfully. Zero typically means successful execution.
void is possible but non-standard. The returned int is meant to signify something for the caller.
The same way as a function in your program may returns values to indicate its result, main returns to indicate the result of execution of your program.
Also in case int main() does return explicit, compiler will put return 0 automatically

Recursive call on main C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can main function call itself in C++?
I found this problem very interesting but illusive a bit.
Question 6.42 C++ how to program by Dietel "Can main be called recursively on your system? write a program containing a function main. Include Static local variable count and initialize to 1. Post-increment and print the value of count each time main is called. Compile your program. What happens ?
I wrote the program as below but instead I made the recursion stops after 10 times as if I were to keep it running it will stops at a value around 41000.
my question: how is it legal to call recursively main function in c++, should this program be executed to stack over flow or memory fault, etc.. ? Please explain.
#include <iostream>
using namespace std;
int main()
{
static int count = 0;
count++;
if(count <= 10) {
cout << count << endl;
return main(); //call main
}//end if
system("pause");
return 0;//successful completion
}//end main
thank you
How is it legal to call the main() function recursively in C++
It is not legal. The C++ language standard states that "The function main shall not be used within a program" (C++11 §3.6.1/3). Calling the function is a form of "use."
Any program that calls main() exhibits undefined behavior (technically, such a program is ill-formed because the rule being violated is a diagnosable semantic rule, though I'd be surprised if most compilers rejected the program). Note that this does not prevent the runtime infrastructure that starts your program from calling the main() function.