473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Dying words... exit atexit abort


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
Jul 22 '05 #1
8 2815
On Sat, 25 Sep 2004 23:22:17 +0000, 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:


(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.

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #2

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:J4*******************@news.indigo.ie...

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
Jul 22 '05 #3
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

Jul 22 '05 #4
> 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

Jul 22 '05 #5
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...
}
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #6
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.
-JKop
Jul 22 '05 #7
JKop wrote:
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 ?

--
Karthik.
http://akktech.blogspot.com .
Jul 22 '05 #8
Karthik Kumar posted:
JKop wrote:
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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Maxwell Hammer | last post by:
An application I am developing executes many threads and then has a "monitor" part that waits for certain events. One of these events causes the application to have to shutdown. On shutdown the...
17
by: jwaixs | last post by:
Hello, I was wondering, what's the difference between exit and return in the main() function? For me they both look the same, or aren't they? And if they aren't, which should I use in which...
7
by: Dave Rudolf | last post by:
Hi all. I have a multithreaded little app that I am writing. Periodically, I see messages on my console that read: The thread '<No Name>' (0xa7c) has exited with code 0 (0x0).
8
by: Martin Eisenberg | last post by:
Hi, If I want to terminate a program upon finding that sprintf has overrun its output buffer, should I prefer exit or abort from cstdlib? Thanks. Martin --
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
3
by: carl.dhalluin | last post by:
Hi, I am playing with the atexit module but I don't find a way to see the difference between a script calling sys.exit(<returncode>) and the interpreting arriving at the end of the source code...
16
by: Laurent Deniau | last post by:
I would like to know if the use of the pointer ref in the function cleanup() below is valid or if something in the norm prevents this kind of cross-reference during exit(). I haven't seen anything...
9
by: titanandrews | last post by:
Hi All, Is there any way to catch the exit code from someone calling exit(status) and check the value before the program terminates? Just for an idea, the code I'm thinking of is something...
11
by: Rahul | last post by:
Hi Everyone, I have seen code in different styles like main(argc,argv) int argc; char **argv; { if(argc != 2) exit(1); else exit(0);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.