Return, exit, and abort in C ++

Source: Internet
Author: User

Exit ():

During the call, most of the cleaning work will be done, but partial objects will never be destroyed because there is no stack unwinding.

The cleanup process includes destroying all static and global objects, clearing all buffers, and disabling all I/O channels. Before termination, the system calls the function that is logged on through atexit (). If an exception is thrown by atexit, the system calls terminate ().

 

Abort ():

During the call, no cleanup is performed. Terminate the program directly.

 

Retrun:

During the call, stack unwinding is performed, local object destructor are called, and local objects are cleared. If it is in main, the system will call exit () later ().

 

Example:

Transferred from: GNU. GCC. Help discussion group

(Of course, because of the spirit of the Chinese nation from the wall, it is better to reprint the source code ...)

By Alex

 

######################################## ######
########### 1.1 The foo_aaa.h file ##########
######################################## ######

# Include <string>
# Include <assert. h>

//----------
Class aaa
{
PRIVATE:
Static int count_s;
Int number _;
String name _;

Public:
AAA ();
AAA (const string & name_ I );
~ AAA ();

};

######################################## ######

######################################## ######
########### 1.2 The foo_aaa.c file ##########
######################################## ######

# Include "foo_aaa.h"

//----------
Int AAA: count_s (0 );

//----------
// Constructor-0
AAA: AAA ()
{
// Empty

}

//----------
// Constructor-1
AAA: AAA (const string & name_ I)
{
Name _ = name_ I;
Number _ = ++ count_s;
Cout <"constructor called: name #"
<Number _
<"="
<Name _
<Endl;

}

//----------
// Destructor
AAA ::~ AAA ()
{
Cout <"destructor called: name #"
<Number _
<"="
<Name _
<Endl;

}

######################################## ######

######################################## ######
########### 1.3 The foo_main.c file #########
######################################## ######

# Include "foo_aaa.h"

//----------
AAA obj1 ("Global ");

//----------
Int main (INT argc, char ** argv)
{
Cout <"/tprogram" <argv [0] <"-> started" <Endl;
Const string return_termination = "return ";
Const string exit_termination = "exit ";
Const string abort_termination = "Abort ";

If (! (Argc = 2 )&&
(Argv [1] = return_termination) |
(Argv [1] = exit_termination) |
(Argv [1] = abort_termination)
)
))
{
Cout <"Usage :"
<Argv [0]
<"Termination_type"
<Endl;

Cout <"---"
<Return_termination
<Endl;

Cout <"---"
<Exit_termination
<Endl;

Cout <"---"
<Abort_termination
<Endl;

Cout <"/tprogram"
<Argv [0]
<"-> Fatal termination"
<Endl;

Assert (0 );
}

String termination_status = argv [1];
// ==================================
AAA obj2 ("local_auto ");
Static AAA obj3 ("local_static ");
// ==================================

Cout <"/tprogram"
<Argv [0]
<"-> Finished :"
<Termination_status
<Endl;

If (termination_status = return_termination)
{
Return 0;
}

If (termination_status = exit_termination)
{
Exit (0 );
}

If (termination_status = abort_termination)
{
Abort ();
}

Assert (0 );

} // Int main (INT argc, char ** argv)

######################################## ######

######################################## ######
########### 2.1 A. Out return ################
####### The results of the running ##########
######################################## ######

Constructor called: name #1 = Global
Program A. Out-> started
Constructor called: name #2 = local_auto
Constructor called: name #3 = local_static
Program A. Out-> finished: Return
Destructor called: name #2 = local_auto
Destructor called: name #3 = local_static
Destructor called: name #1 = Global

// ========= Conclusion =======
// All Destructors are called
// ==================================

######################################## ######
########### 2.2 A. out exit ##################
####### The results of the running ##########
######################################## ######

Constructor called: name #1 = Global
Program A. Out-> started
Constructor called: name #2 = local_auto
Constructor called: name #3 = local_static
Program A. Out-> finished: Exit
Destructor called: name #3 = local_static
Destructor called: name #1 = Global

// ========= Conclusion =======
// The local_auto object destructor isn't called
// ==================================

######################################## ######
########### 2.3 A. Out abort #################
####### The results of the running ##########
######################################## ######

Constructor called: name #1 = Global
Program A. Out-> started
Constructor called: name #2 = local_auto
Constructor called: name #3 = local_static
Program A. Out-> finished: Abort

// ========= Conclusion =======
// No destructor is called
// ==================================

 

 

 

Postscript:

Unrelated to programming

 

 

Appreciation of Lian chengxi:

Excerpted from the original article Chapter 1: wall building

 

 

The lights in the room are turned off, but a strange sound is coming. "Hey, hey, hey !" It seems that someone is doing something with great effort ....... Go to the room from the window. At that time, the moonlight oblique, through the window paper, reflected in the room, saw Wan Zhenshan lying in bed, hands slowly push to the air, but his eyes closed tightly.
......
I saw wanzhenshan's hands pushed for a while, slowly got up, stretched out of bed, walked a few steps forward, squatted down, lingkong reached out to catch what thing ....... When I saw the film again, I saw that the gesture of wanzhenshan was getting more and more strange. I couldn't help but capture anything in the air. Then I arranged it neatly together, but it seemed like I placed many bricks in a stack, but in the moonlight, I can see that the floor is empty.
......
I saw him grasp the space for a while, his hands compared to a comparison, it seems that big enough, so his hands in the ground to hold a large thing, pushed forward.
......
When wanzhenshan put all the empty loads into the empty wall holes, lingkong pushed a few times, then picked up all the bricks in the ground and built the walls.
Yes, he's really building a wall! A smile on your face and a proud wall!
......
However, when wanzhenshan put all the "Bricks" removed into the "wall hole", the "Lime" will be refreshed until the "Kung Fu" is done properly, go to bed.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.