#include <iostream>
void onExit( void )
{
// Run cleanup code here!
}
int main()
{
// Register the function that will be called
// when the program exits
atexit(onExit);
// Do stuff
return 0;
}
Or you could develop an application class, and put cleanup code in its destructor.
Grey Wolf, when declaring a function with no parameters, do not put void, simply put nothing in between the parentheses. It's a common thing that some people do, but is bad coding (like void main() instead of int main())
Thank you for your input but there is nothing wrong with using void as a parameter declaration clause. For your reference see section 8.3.5 paragraph 2 of the standard "...The parameter list (void) is equivalent to the empty parameter list...".
The reason I put void as the parameter declaration clause, was to highlight the fact that the function that is passed into the atexit() function must not take any parameters (I forgot to put that in the comments).
I heard that this was a difference in C/C++ standards. I'm not all that familiar with C, but a friend of mine on another forum said that an empty parameter list was valid in both C/C++, but meant different things. On C++ it meant the function takes no parameters ( like func(void) ), but on C it meant the function could take any number of parameters ( like func(...) ).
He quoted "section 3.5.4.3 of the C89 standard" -- which I didn't bother to look up. Personally I took it with a grain of salt -- seems kind of silly to me.
In that case I can see someone wanting to stick to the func(void) mentality for clarity or if they're just used to C.
I can't comment on the C89 Standard, but for C99 says
If the list terminates with an ellipsis (, ...), no information about the number or types of the parameters after the comma is supplied.
and
The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
and
An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.
So
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int func1(); /* Declares func1() but gives no info on parameters */
int func2(void); /* Declares a function that takes no parameters */
int func3() /* Declares and defines a function that takes no parameters */
{ /**/ }
/*
**
**
*/
int func1(int x)
{/*...*/}
int func2(void)
{/*...*/}
NB: Head compiler used, code might not be entirely accurate. ;0)
No, it definitely doesn't kill the process. I've seen ping terminating cleanly when closing the console. My guess is cmd sends the TERM signal (or its equivalent) to whatever is running on it.
Thinking about it, kbw and helios are probably both correct. Windows would most likely sent a signal to the app to close and if it doesn't respond Kill it (or at least pop up a dialog asking if it can kill it).