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

  • Home
  • SEARCH
  • 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 6370981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:00:03+00:00 2026-05-25T01:00:03+00:00

In what order get the constructors of global object in C++ called? This question

  • 0

In what order get the constructors of global object in C++ called?

This question arises in the context of a memory pool that manages the memory needs for some consumers. I was presented with a rather large source code which defines at global namespace some consumers just using heap functions. A memory pool should be added without changing the namespace of the consumers. So, I added a pool class and a definition at global namespace and modified each consumer class to get memory from the instance of the class “thePool”. Unfortunately at the end of the execution when it comes to call all global destructors I get a nice segfault. gdb backtrace shows that the branch to pool::free produces the segfault. This sounds odd to me. However, I boiled it down to a very simple pool/consumer example which lives on the global namespace. Unfortunately this does not reproduce the segfault — the destructor of pool gets called after all destructors of the consumer. Is this pure luck, or a well-educated guess of g++ 4.5 ?

Here the boiled down example:

#include<iostream>

using namespace std;


struct pool {
  pool() {
    cout << "pool::pool" << endl;
  }
  ~pool() {
    cout << "pool::~pool" << endl;
  }
  void get() {
    cout << "pool::get" << endl;
  }
  void free() {
    cout << "pool::free" << endl;
  }
};

pool thePool;


struct consumer {
  ~consumer() {
    cout << "consumer::~consumer" << endl;
    thePool.free();
  }
  consumer() {
    cout << "consumer::consumer" << endl;
    thePool.get();
  }
};



consumer a,b;

int main() {
}

The output is:

pool::pool
consumer::consumer
pool::get
consumer::consumer
pool::get
consumer::~consumer
pool::free
consumer::~consumer
pool::free
pool::~pool

Nice! Just as I wanted it. Pure luck? I mean the dtor of pool could have been called before destroying consumer a or b, right?

The question to answer is: “In what order are the cons of global objects called?”

  • 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-05-25T01:00:04+00:00Added an answer on May 25, 2026 at 1:00 am

    Global variables are initialized in the order in which they’re declared. So their constructor will be called in the same order in which they’re initialized. This is true within one translation unit. However, the initialization order across multiple translation-units is not defined by the language specification.

    And their destructors are called in the reverse order of their initialization as usual.

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

Sidebar

Related Questions

In order to get the next element in the list one just needs to
How to fix this regex in order to get the mentioned outcome ? regex:
This test is to check that I can return a ViewModel object by creating
I know that in order to create an object of ArrayList type I need
This is an Nhibernate question that has bothered me for some time... If I
I have this situation: public class busOrder: IbusOrder { public Order vOrder { get;
In order to get my setup a bit closer to one click deployment, I
In order to get better SEO and cleaner URLs, I tend to export certain
I'm using System.Xml in order to get data from xml data: string test =
In cake php is how we can get order of query result according to

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.