What is the difference between stop and exit in Fortran?
Both can terminate the program immediately with some error information.
exit in Fortran is a statement which terminates loops or completes execution of other constructs. However, the question is clearly about the non-standard extension, as either a function or subroutine, offered by many compilers which is closely related to the stop statement.
For example, gfortran offers such a thing.
As this use of exit is non-standard you should refer to a particular implementation's documentation as to what form this takes and what effects it has.
The stop statement, on the other hand, is a standard Fortran statement. This statement initiates normal termination of execution of a Fortran program (and can be compared with the error stop statement which initiates error termination).
Other than knowing that terminating (normally) execution of the program follows a stop statement and that there is a stop code, the actual way that happens are again left open to the implementation. There are some recommendations (but these are only recommendations) as to what happens. For example, in Fortran 2008 it is suggested that
the stop code (which may be an integer or a string) be printed to an "error" unit;
if the stop code is an integer it be used as the process exit status;
if there is no stop code (or it's a character) zero be returned as the exit status.
The above is fairly vague as in many settings the above concepts don't apply.
Typically in practice, exit will be similar to the C library's function of that name and its effect will be like stop without a stop code (but still passing the given status back to the OS).
In summary, Fortran doesn't describe a difference between stop and exit. Use of exit (for termination) is non-portable and even the effect of stop is not entirely defined.
stop is a fortran statement but exit is a function that just happens to terminate the program.
The stop statement will output its argument [which can also be a string] to stderr
stop 123
and it will return a zero status to the parent process.
Whereas exit is a function and must be called like any other. It will also be silent (i.e. no message):
call exit(123)
and the argument to exit will be returned to the parent process as status
Related
We have a large Fortran application which calls many C++ modules. I am trying to use the C++ objects' destructors to free resources and close files, but it seems they are not being called when the Fortran program exits.
The Fortran program exits using the STOP command.
Do I need to use a different Fortran command to exit, or call the C++ exit(0) command from Fortran?
To get proper construction/destruction, you just about need the entry point to be on the C++ side.
At least offhand, the simplest approach I can think of that seems at all likely to work would be something like this:
set up main in C++, and have it as the entry point.
move your current Fortran entry point into a function.
call that function from main
write a small function in C++ named something like do_stop() that just throws an exception
in your Fortran, replace STOP with calls to do_stop().
You can either leave the exception uncaught, or have a try/catch in main, which can give a slightly more graceful exit (display an error message of your choice instead of something written by the library author saying your program has an error).
I just was looking up funciton attributes for gcc
(http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html)
and came across the returns_twice attribute.
And I am absolutely clueless in what case a function can return twice... I looked up quickly the mentioned vfork() and setjmp() but continue without an idea how an applicable scenario looks like - anyone of you used it or can explain a bit?
The setjmp function is analogous to creating a label (in the goto sense), as such you will first return from setjmp when you set the label, and then each time that you actually jump to it.
If it seems weird, rest assured, you should not be using setjmp in your daily programming. Or actually... you should probably not be using it at all. It is a very low-level command that break the expected execution flow (much like goto) and, especially in C++, most of the invariants you could expect.
When you call setjmp, it establishes that as a return point, then execution continues at the code immediately following the setjmp call.
At some point later in the code, calling longjmp (with the jump buffer initialized by the previous call to setjmp) returns execution to start from that same point again (i.e., the code immediately following the call the setjmp).
Therefore, the original call returns normally, then at arbitrary later times, execution returns (or at least may return) to the same point again.
The attribute simply warns the compiler of that fact.
Here is the code (valid C and C++)
#include <stdio.h>
int main() {
printf("asfd");
// LINE 1
return 0;
}
If in line 1 I put segfaulting expression the program would just crash without printing anything (as expected).
But why is the above code printing "asdf" and not exiting without buffer being flushed? What is under the hood and why does it work as expected?
This is accomplished by these two sections in the C++ language specification:
[basic.start.main]
A return statement in main has the effect of leaving the main function and calling exit with the return value as the argument.
and
[lib.support.start.term]
The function exit has additional behavior in this International Standard:
...
Next, all open C streams with unwritten buffered data are flushed.
...
Generally, a return from main is not the end of your program, nor is entry to main the start.
Usually, the linker that creates the final executable for your program marks some location, perhaps named start, as the place where execution is to begin. When the operating system loads your program and starts executing it, it starts execution at this place. There is code there that sets up an environment: Creates a stack, sets stream states, et cetera. Then this code calls main.
When main returns, it returns to this special code. That code then performs various clean-up work that is required at the end of a C or C++ program, as described in this answer.
If a program is terminated abruptly, this final code might not be executed.
When main() exits, all open streams are closed... to include stdout. Closing the open stream flushes stdout and what you've written to the buffer gets committed with or without the newline.
I currently have a program which has the following basic structure
main function
-- displays menu options to user
-- validates user input by passing it to a second function (input_validator)
-- if user selects option 1, run function 1, etc
function1,2,3,etc
-- input is requested from user and then validated by input_validator
-- if input_validator returns true, we know input is good
Here is my problem. I want to allow the user to quit at any point within the program by typing '0'. I planned on doing this with some basic code in input_validator (if input = 0, etc).
This would appear to be simple, but I have been told that using quit() will result in some resources never been released / etc. I cannot simply do a 'break' either -- it will result in my program simply returning to the main function.
Any ideas?
One possibility would be to do it by throwing an exception that you catch in main, and when you catch it, you exit the program. The good point of throwing an exception is that it lets destructors run to clean up objects that have been created, which won't happen if you exit directly from elsewhere (e.g., by using exit()).
exit()
Terminates the process normally,
performing the regular cleanup for
terminating processes.
First, all functions registered by
calls to atexit are executed in the
reverse order of their registration.
Then, all streams are closed and the
temporary files deleted, and finally
the control is returned to the host
environment.
This hasn't been true for any kind of mainstream operating system for a long time. The OS ensures that all kernel resources are released, even if the program didn't explicitly do so. Calling abort() or exit() from anywhere in your code is fine.
exit(int exitCode) - defined in stdlib.h / cstdlib - you'd probably want to exit(0); // normal termintation.
exit() will not call your destructors, so you might want to consider using an exception handler instead.
If you have things like open but unflushed files, the OS will close the file handles, but won't flush any unwritten data.
You have to design your menu system so that a status can be passed back to the previous method, unwinding until code in the main function is executed. Similar issues apply to back or previous screen buttons.
Taking a step back and looking at the Big Picture, the unwinding technique looks very similar to C++ exception handling strategy. I suggest using exceptions for cases that don't follow the normal flow of execution, such as main menu, and previous menu.
Try it out.
The getche() function doesn't terminate the program properly, so I want to try exit(int status) function. How does it work in Turbo C++ programming language? I cannot understand the explanation in related help modules and I seek for a better explanation... E.g. what does function's parameter consist of? Thanks in advance!
Not 100% sure what you mean by this. The parameter to the exit function (the "int status") is a number that gets returned to the shell. Traditionally, this is zero if your program was successful and >0 if the program failed for some reason. The function will clean up various things and then exit your program.
Well. Functions like getch.. usually get a character from the keyboard or standard input. They are sometimes used at the end of programs like this
int main() {
// do many stuff...
// ...
getch();
}
The getch/getche (i don't know what the e stands for in turbo-c++) are then used to give the user the change to see the output of the program, before the terminal windows closes (usually that happens in windows). Note that there is a portable function called getchar in C and C++ that does also do that job (waits for an enter in addition, but that won't cause a harm here).
But it is not used to terminate the program. After a key is pressed, control continues and then after main finished, the program exists. C++ and recent C versions insert a return 0; implicitly after the last statement of the main function (0 stands for "succesful"). This means your main function returns a value of 0 back to the OS. But you can return other values if you write the return explicitly and put another value there. That value is what exit expects. It terminates your program, and returns the given value back to the OS.
int main() {
// some stuff...
exit(42);
// other stuff (note: never reached!)
}
That program will return a value of 42 to the OS. Normally you just return 42; there and it has the same effect (*).
(*) Well, not entirely: If you have local variables, the destructor of those are not called if you use exit. But they are cleanly destructed and destructors are called when you use return n;. Therefor, prefer return n; in main when you can. exit called in other functions than your main will terminate your program too, so it can be required to use that instead, because return there will just return from those specific functions and do not at all terminate the program.
The parameter is an integer status code that is passed back to the invoking shell as the exit status of the process. Exit itself should exit the process an clean up any open resources that your process is using.
The parameter to Exit() is an int that indicates the reason or status of the exiting process.
Boy, that didn't sound good. Let me try again: It's a value that is passed back to either the starting process (the process that used CreateProcess() or ShellExecute() to launch the one that's exiting), or, in the case of a console app a value to the command shell that can be accessed via ERRORLEVEL.
It's typical to set status = 0 if you're exiting normally, another value (that can have meaning if you want to the process that receives it) to indicate errors or problems.
The parameter is an integer status code that is passed back to the invoking shell as the exit status of the process. Exit itself should exit the process an clean up any open resources that your process is using.
More Info on "exit" Function
"exit" function Terminates the process normally, performing the regular cleanup for terminating processes.
First, all functions registered by calls to "atexit" are executed in the reverse order of their registration. Then, all streams are closed and the temporary files deleted, and finally the control is returned to the host environment.
The value supplied as an argument to exit is returned to the operating system ( the host environment) as the program's return code or exit code. By convention, a return code of zero means that the program completed successfully.
Hope this clears your doubt.