473,696 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("PA USE");
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("PA USE");
}
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("PA USE");
}

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

Any thoughts on this?
-JKop
Jul 22 '05 #1
8 2831
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("PA USE");
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)(voi d));

....

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******** ***********@new s.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("PA USE");
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("PA USE");
}
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("PA USE");
}

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

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("PA USE");
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("PA USE");
}
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("PA USE");
}

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

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.merke rk(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("PA USE");
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("PA USE"); }
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("PA USE");
}

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

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("PA USE");
I've looked up "exit", "atexit" and "abort". Up until this point I
simply would've done:

int main()
{

End_of_pro g:
std::cout << "The program will now end.\n";
std::system("PA USE"); }
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("PA USE");
}

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

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("PA USE");
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("PA USE"); }
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("PA USE");
}

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

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
1887
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 monitor part notifies the threads of a shutdown, and the threads must cleanup and exit. When all threads have exited the monitor itself exits. The traceback below occured on a shutdown. At the time there was only one thread running, however the...
17
8055
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 situation? Also I was wondering if it whould be wise to combine the standard status with return. exp:
7
3545
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
2824
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
3590
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 exit(n)) statement provide both function call and return features of the C programming language? /* headers omitted */ int main (void)
3
3323
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 file. This has a semantic difference for my applications. Is there a way to determine in an exithandler (that is registered using atexit.register)
16
3608
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 in the norm against this, I mean an as-if rule saying "atexit registered functions are executed as-if they were called from main", making val out of scope at this point. a+, ld. #include <stdio.h>
9
11572
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 like this: void exithandler() { DWORD exitCode = 0;
11
2837
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
8661
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9142
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8849
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7699
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5855
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2313
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.