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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:45:15+00:00 2026-05-20T03:45:15+00:00

I am writing a library and wanted to be as C++ centric as possible,

  • 0

I am writing a library and wanted to be as C++ centric as possible, remembering the old adage “Macros are evil”.

In a source file, I had the following definitions:

const std::string DATA_DIR_ENV_STR = "DATADIR"
const std::string DFLT_DATA_DIR =  "../data"

/*
#define DATA_DIR_ENV_STR  "DATADIR"
#define DFLT_DATA_DIR  "../data"
*/


std::string getRootDirectory()
{
    char * datastr_ = getenv(DATA_DIR_ENV_STR);

    if (datastr_)
        return std::string(datastr_);
    return DFLT_DATA_DIR;
}

// Header file

std::string getRootDirectory();

I then had a singleton class that was initialized like this:

bool mySingleton::inited = mySingleton::initialize();

bool mySingleton::initialize(){
   std::string rootdir = getRootDirectory(); // <-SEGV when using const std::string
}

The library compiled fine, but when I linked an application to it, the app always SEGV’d. I used gdb to track down the problem and to my shock/horror, the string variables DATA_DIR_ENV_STR and DFLT_DATA_DIR had not yet been initialized when they were been accessed during the static variable initialization.

In the end I simply used macros to get around the issue. BUT, I can’t help wondering, is this a variation of the ‘static variable initialization fiasco’?. Is there another way to resolve this without using macros?

  • 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-20T03:45:15+00:00Added an answer on May 20, 2026 at 3:45 am

    Yes, this is the static initialization fiasco biting your behind.

    A way to avoid it is the “construct on first use” idiom (brain-compiled code):

    // In the header
    class mySingleton {
        private:
            static mySingleton *s_instance;
            mySingleton();
        public:
            mySingleton &instance() {
                if (!s_instance)
                    s_instance = new mySingleton();
                return *s_instance;
            }
    };
    
    // And in the source file...
    mySingleton *mySingleton::s_instance;
    

    However, in the case of string constants, you can also get away with simple char pointers:

    static char const *const DATA_DIR_ENV_STR = "DATADIR";
    static char const *const DFLT_DATA_DIR = "../data";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a library which uses a few functions from the windows user32.dll
I have been writing a library for my project (for now I am using
Ok I am writing a library that will be shard between unix and windows.
I'm writing a library for mobile devices. This library notifies the users when certain
I am writing a library that will provide a collection of public types to
I am writing a library to generate PDF reports using prawn reports. One of
I am writing a library that creates multiple elements inside a wrapper element, and
I'm writing a library search engine where a user can search based on various
I am writing a library which is a set of classes meant to be
Say that you're writing a library to display things on the screen, so you

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.