Dying words... exit atexit abort

J

JKop

Let's say that when your program ends (no matter how) that you want a
certain block of code to be executed at the end. Here's the code:

std::cout << "The program will now end.\n";
std::system("PAUSE");


I've looked up "exit", "atexit" and "abort". Up until this point I simply
would've done:

int main()
{

End_of_prog:
std::cout << "The program will now end.\n";
std::system("PAUSE");
}


and used goto statements instead of return statements. But at the moment I'm
thinking of switching to:

#include <iostream>

void DyingWords()
{
std::cout << "The program will now end.\n";
std::system("PAUSE");
}

int main(int argc, char* argv[])
{
atexit(DyingWords);
}



Any thoughts on this?


-JKop
 
O

Owen Jacobson

Let's say that when your program ends (no matter how) that you want a
certain block of code to be executed at the end. Here's the code:

std::cout << "The program will now end.\n";
std::system("PAUSE");


I've looked up "exit", "atexit" and "abort". Up until this point I simply
would've done:

(snip)

From my collection of man pages:

NAME
atexit - register a function to be called at normal program termination
^^^^^^

SYNOPSIS
#include <stdlib.h>

int atexit(void (*function)(void));

....

CONFORMING TO
SVID 3, BSD 4.3, ISO 9899, POSIX 1003.1-2001
^^^^^^^^
Looks okay to me; that's the ISO C Programming Lanugage, as well as POSIX
and BSD.

Be aware that abort() may or may not trigger the atexit handlers and is
used by most implementations of assert, and that unhandled exceptions may
or may not trigger atexit handlers, and that atexit() itself can fail.
 
C

Chris Theis

JKop said:
Let's say that when your program ends (no matter how) that you want a
certain block of code to be executed at the end. Here's the code:

std::cout << "The program will now end.\n";
std::system("PAUSE");


I've looked up "exit", "atexit" and "abort". Up until this point I simply
would've done:

int main()
{

End_of_prog:
std::cout << "The program will now end.\n";
std::system("PAUSE");
}


and used goto statements instead of return statements.

Isn't it funny that you consider macros as a manifestation of evil and use
constructs like goto??? ;-)
But at the moment I'm
thinking of switching to:

#include <iostream>

void DyingWords()
{
std::cout << "The program will now end.\n";
std::system("PAUSE");
}

int main(int argc, char* argv[])
{
atexit(DyingWords);
}



Any thoughts on this?

In principle I'd say atexit is the way to go but there are some subtle
issues regarding exception handling. Look up also abort() & terminate() and
read the standard's chapters 3.6.3, 15.5.1, and 18.3.

Regards
Chris
 
J

JKop

Chris Theis posted:
Isn't it funny that you consider macros as a manifestation of evil and
use constructs like goto??? ;-)


Ain't nothing wrong with "goto"!

In principle I'd say atexit is the way to go but there are some subtle
issues regarding exception handling. Look up also abort() & terminate()
and read the standard's chapters 3.6.3, 15.5.1, and 18.3.


Thanking you,


-JKop
 
J

JKop

Be aware that abort() may or may not trigger the atexit handlers and is
used by most implementations of assert, and that unhandled exceptions
may or may not trigger atexit handlers, and that atexit() itself can
fail.

Isn't clarity a beautiful thing:


4 Calling the function
void abort();
declared in <cstdlib> terminates the program without executing destructors
for objects of automatic or
static storage duration and without calling the functions passed to atexit
().


-JKop
 
P

Peter van Merkerk

JKop said:
Let's say that when your program ends (no matter how) that you want a
certain block of code to be executed at the end. Here's the code:

std::cout << "The program will now end.\n";
std::system("PAUSE");


I've looked up "exit", "atexit" and "abort". Up until this point I simply
would've done:

int main()
{

End_of_prog:
std::cout << "The program will now end.\n";
std::system("PAUSE");
}


and used goto statements instead of return statements. But at the moment I'm
thinking of switching to:

#include <iostream>

void DyingWords()
{
std::cout << "The program will now end.\n";
std::system("PAUSE");
}

int main(int argc, char* argv[])
{
atexit(DyingWords);
}

Any thoughts on this?

An alternative way of doing this (untested code):

class GoodBye
{
public:
~GoodBye()
{
std::cout << "That's all folks!.\n";
}
};

int main(int argc, char* argv[])
{
GoodBye gb;

// Your stuff goes here...
}
 
J

JKop

Peter van Merkerk posted:
JKop said:
Let's say that when your program ends (no matter how) that you want a
certain block of code to be executed at the end. Here's the code:

std::cout << "The program will now end.\n"; std::system("PAUSE");


I've looked up "exit", "atexit" and "abort". Up until this point I
simply would've done:

int main()
{

End_of_prog:
std::cout << "The program will now end.\n";
std::system("PAUSE"); }


and used goto statements instead of return statements. But at the
moment I'm thinking of switching to:

#include <iostream>

void DyingWords()
{
std::cout << "The program will now end.\n"; std::system("PAUSE");
}

int main(int argc, char* argv[])
{
atexit(DyingWords); }

Any thoughts on this?

An alternative way of doing this (untested code):

class GoodBye
{
public:
~GoodBye()
{
std::cout << "That's all folks!.\n";
}
};

int main(int argc, char* argv[])
{
GoodBye gb;

// Your stuff goes here...
}


I'd prefer a global object to be honest!

That way, even if "exit" is called, its constructor is still invoked.


-JKop
 
K

Karthik Kumar

JKop said:
Peter van Merkerk posted:

JKop wrote:

Let's say that when your program ends (no matter how) that you want a
certain block of code to be executed at the end. Here's the code:

std::cout << "The program will now end.\n"; std::system("PAUSE");


I've looked up "exit", "atexit" and "abort". Up until this point I
simply would've done:

int main()
{

End_of_prog:
std::cout << "The program will now end.\n";
std::system("PAUSE"); }


and used goto statements instead of return statements. But at the
moment I'm thinking of switching to:

#include <iostream>

void DyingWords()
{
std::cout << "The program will now end.\n"; std::system("PAUSE");
}

int main(int argc, char* argv[])
{
atexit(DyingWords); }

Any thoughts on this?

An alternative way of doing this (untested code):

class GoodBye
{
public:
~GoodBye()
{
std::cout << "That's all folks!.\n";
}
};

int main(int argc, char* argv[])
{
GoodBye gb;

// Your stuff goes here...
}



I'd prefer a global object to be honest!

That way, even if "exit" is called, its constructor is still invoked.

Did u mean to say *destructor* here ?
 
J

JKop

Karthik Kumar posted:
JKop said:
Peter van Merkerk posted:

JKop wrote:


Let's say that when your program ends (no matter how) that you want a
certain block of code to be executed at the end. Here's the code:

std::cout << "The program will now end.\n"; std::system("PAUSE");


I've looked up "exit", "atexit" and "abort". Up until this point I
simply would've done:

int main()
{

End_of_prog:
std::cout << "The program will now end.\n";
std::system("PAUSE"); }


and used goto statements instead of return statements. But at the
moment I'm thinking of switching to:

#include <iostream>

void DyingWords()
{
std::cout << "The program will now end.\n"; std::system("PAUSE");
}

int main(int argc, char* argv[])
{
atexit(DyingWords); }

Any thoughts on this?

An alternative way of doing this (untested code):

class GoodBye
{
public:
~GoodBye()
{
std::cout << "That's all folks!.\n"; } };

int main(int argc, char* argv[])
{
GoodBye gb;

// Your stuff goes here... }



I'd prefer a global object to be honest!

That way, even if "exit" is called, its constructor is still invoked.

Did u mean to say *destructor* here ?

Yep!

-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top