Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:24:18+00:00 2026-06-03T05:24:18+00:00

I have the following program where i am calling exit() in the destructor. When

  • 0

I have the following program where i am calling exit() in the destructor. When i create an object of type sample inside main() destructor is called once and program exits normally. But when i create a global object of type sample, “Destructing..” gets printed infinitely. Can anyone please explain how?

#include "iostream"
#include "conio.h"

using namespace std;

class sample
{
      public:
     ~sample() {
                    cout <<"Destructing.."<<endl;
                    exit(0);
                }
};

sample obj;

int main()
{
 getch();   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:24:20+00:00Added an answer on June 3, 2026 at 5:24 am

    What’s happening is, the exit() function is getting the program to call destructors on all of the global objects. And since at the point where your class’s destructor calls exit(1); the object is not yet considered to be destructed, the destructor gets called again, resulting in an infinite loop.

    You could get away with this:

    class sample {
        bool exiting;
    public:
        sample() { exiting = false; }
        ~sample() {
            cout << "Destructing.." << endl;
            if(exiting) return;
            exiting = true;
            exit(0);
        }
    };
    

    But having a destructor call exit() is a Bad Idea. consider one of these alternatives:

    • create a separate normal (non-destructor) method for exiting
    • create a function that runs until the “program” is finished and call it from main()
    • use abort() instead of exit() (Thanks goldilocks for mentioning this one). abort() bypasses all of the cleaning up that is normally done when exit() is called and when main() returns. However this is not necessarily a good idea either, as certain cleanup operations in your program could be quite critical. abort() is meant only for errors that are already so bad that bypassing cleanup is warranted.

    I suggested exceptions before but remembered about throwing exceptions from inside destructors and changed my mind. here’s why.

    Note also, the behavior is not consistent – some compilers/environments result in an infinite loop, some don’t. It comes down to at which point in the destructor the object is considered to be destroyed. I would guess the standard either doesn’t cover this or says the behavior in this case is undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function, that is called once in my program. When there
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have the following program #include <stdio.h> int main(void) { unsigned short int length
I have the following program: ~/test> cat test.cc int main() { int i =
Hi I have the following program. When I compile on the terminal gcc main.c
I have the following program. It is object oriented, having a structure 'array' (I
I have found following question: $0 (Program Name) in Java? Discover main class? but
I have the following program in which I'm trying to understand the functioning of
I have the following program to open lot's of sockets, and hold them open
I have the following program to browse all virtual directories and their sub directories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.