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 6616983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:38:58+00:00 2026-05-25T20:38:58+00:00

I have a vector in a header, like so: extern std::vector<Foo> g_vector; In the

  • 0

I have a vector in a header, like so:

extern std::vector<Foo> g_vector;

In the associated cpp file I have this:

std::vector<Foo> g_vector;

I also have a class Bar, and in it’s constructor it will add some stuff to g_vector, like so:

Bar::Bar(/* stuff */)
{
    // do things
    std::cout << g_vector.size() << std::endl;
    g_vector.push_back(somefoo);
    std::cout << g_vector.size() << std::endl;
}

If I declare a Bar inside a function, like a sane person, it appears to work fine. However, if I want to declare a Bar outside of a function, weird things happen. For example, I have a Bar declared in MyFile1.cpp and in MyFile2.cpp, and because of my cout statements in Bar I can see the Foo get pushed into the vector, but when the next Bar runs its constructor the vector’s size is 0 again. In other words, my output is

0
1
0
1

What gives? Just to be extra double sure, I also tried printing out &g_vector to make sure it was actually push_backing into the right vector, and the addresses all match. For what it’s worth, it doesn’t matter what order these things go in to the vector. I’m not concerned with the initialization order or anything.

  • 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-25T20:38:58+00:00Added an answer on May 25, 2026 at 8:38 pm

    Not sure what the issue really is, but I guess the following pattern will help solve it: define an accessor to the global variable and allocate it as a static function variable as shown below.

    In the header file:

    std::vector<Foo> &getGlobalVector();
    

    In the cpp file:

    std::vector<Foo> &getGlobalVector()
    {
      static std::vector<Foo> s_vector;
      return s_vector;
    }
    

    This pattern is inspired from Andrei Alexandrescu’s “generic singleton” implementation in Modern C++ design.

    I’ve taken the habit of systematically using this pattern whenever I fell upon an existing global variable while maintaining existing applications (or in the rare occasions I actually chose to use one myself), and it may have helped in eliminating a couple of hard-to-reproduce bugs in said applications.

    At any rate, this should really help avoiding any multiple-initialization or order-of-initialization related issue.

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

Sidebar

Related Questions

I have got a map defined inside a class in header file like this:
So I have declared a vector in my class header like this: ... private:
OK so I have a C++ function with a header like this: int myfunc(vector<int>
suppose I have a header foo.h like this: #ifndef FOO_H #define FOO_H #include <string>
I have a header file like so: #pragma once #include gamestate.h #include ExitListener.h class
If I have a definition in a class header such as this: vector<baddie*> baddies;
I have a vector-like class that contains an array of objects of type T
I have a simple class like this: class A { public: void f(const int&
i have shared the header file containing class definition of a Context Free grammar
I have a header file that contains a class. Within that class, I have

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.