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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:08:06+00:00 2026-05-20T21:08:06+00:00

I have an application which [unfortunately] contains quite some global variables. A recent crash-at-startup

  • 0

I have an application which [unfortunately] contains quite some global variables. A recent crash-at-startup lead me to the following construction:

FILE1.CPP:
ClassX globalVariableX;

FILE2.CPP
ClassY globalVariableY;

Unfortunately, the constructor of class Y uses code in which globalVariableX is used.

Until now everything went fine because (coincidentally) FILE1.OBJ was linked in before FILE2.OBJ, meaning that globalVariableX is instantiated before globalVariableY.

Last week, a totally unrelated change in other files, caused the linker to link in FILE2.OBJ before FILE1.OBJ. Now globalVariableY is instantiated first, its constructer indirectly refers to globalVariableX, and crashes because globalVariableX has not been instantiated yet.

I know I should get rid of all the global variables as much as possible (please don’t start a debate about this).

But are there tools available that can help me to look up dependencies between global variables?

Or are there any tricks that I can use to see at run time if there are dependencies which I should get rid of (I was thinking about introducing a base-class for the global variables in which I could log the construction of global variables, but this is probably quite some work). Any other suggestions?

EDIT:
All of your answers are very good suggestions on how to prevent these nasty problems. But I was actually looking for a way to find these dependencies, not removing all global variables (or replacing them with another construction). Any ideas on tools that find these dependencies?

  • 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-20T21:08:06+00:00Added an answer on May 20, 2026 at 9:08 pm

    The usual solution is to rely on initialization on first use.

    In C++, you can use local static (in functions) to get this behavior. In C++0x (but already implemented in major compilers) this is even guaranteed to be thread-safe (the initialization, at least).

    ClassX& GetClassX() {
      static ClassX X; return X;
    }
    
    ClassY& GetClassY() {
      static ClassY Y; return Y;
    }
    

    There is still one issue though: such a scheme does not detect cyclic references. It could be akin to a recursion gone mad and thus blow your stack 😉

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

Sidebar

Related Questions

I have a web page that links to another web application, which unfortunately only
I have an application which supports multiple types and versions of some devices. It
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
I have an application.war folder (not as file) which contains jaxen-1.1.1.jar (in the WEB-INF/lib)
I have a C# (.net 3.5) desktop application which prints out some numbers on
I have a small application which automates Microsoft Word via COM/OLE Automation. Unfortunately this
I have got a WPF Application, which contains a Window, inside it there is
I have application which needs to use a dll (also written by me) which
I have an application which loads up c# source files dynamically and runs them
I have an application which needs to loop through all the lines in text

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.