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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:43:05+00:00 2026-06-13T10:43:05+00:00

Preface: this question is closely related to these ones: … – C++: Avoiding Static

  • 0

Preface:

this question is closely related to these ones: …
– C++: Avoiding Static Initialization Order Problems and Race Conditions Simultaneously
– How to detect where a block of memory was allocated?
… but they have NO positive solution and my actual target use-case is slightly different.

During construction of the object I need to know if it is initialized in static memory bock ( BSS) or is it instantiated in Heap.

The reasons are follow:

  • Object by itself is designed to be initialized to “all zeros” in constructor – therefore no initialization is needed if object is statically initialized – entire block with all objects is already set to zeros when program is loaded.

  • Static instances of the object can be used by other statically allocated objects and alter some member variables of the object

  • Order of initialization of static variables is not pre-determined – i.e. my target object can be invoked before its constructor is invoked, thus altering some of its data, and constructor can be invoked later according to some unknown order of initialization of statics thus clearing already altered data. That is why I’d like to disable code in constructor for statically allocated objects.

  • Note: in some scenarios Object is the subject for severe multi-threaded access (it has some InterlockedIncrement/Decrement logic), and it has to be completely initialized before any thread can touch it – what i can guaranteed if i explicitly allocate it in Heep, but not in static area (but i need it for static objects too).

Sample piece of code to illustrate the case:

struct MyObject
{
    long counter;

    MyObject() {
        if( !isStaticallyAllocated() ) {
            counter = 0;
        }
    }
    void startSomething() { InterlockedIncrement(&counter); }
    void endSomething() { InterlockedDecrement(&counter); }
};

At the moment I’m trying to check if ‘this’ pointer in some predefined range, but this does not work reliably.

LONG_PTR STATIC_START = 0x00400000;
LONG_PTR STATIC_END   = 0x02000000;
bool isStatic = (((LONG_PTR)this >= STATIC_START) && (LONG_PTR)this < STATIC_END));

Update:
sample use-case where explicit new operator is not applicable. Code is ‘pseudo code’, just to illustrate the use-case.

struct SyncObject() {
    long counter;
    SyncObject() { 
        if( !isStaticallyAllocated() ) {
            counter = 0;
        } }
    void enter() { while( counter > 0 ) sleep(); counter++; }
    void leave() { counter--; }
}

template <class TEnum>
struct ConstWrapper {
    SyncObject syncObj;
    TEnum m_value;

    operator TEnum() const { return m_value; }
    LPCTSTR getName() {
        syncObj.enter();
        if( !initialized ) {
            loadNames();
            intialized = true;
        }
        syncObj.leave();
        return names[m_value];
    }
}

ConstWrapper<MyEnum> MyEnumValue1(MyEnum::Value1); 
  • 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-13T10:43:08+00:00Added an answer on June 13, 2026 at 10:43 am

    It looks like after thinking for a while, I’ve found a workable solution to identify if block is in static area or not. Let me know, please, if there are potential pitfalls.

    Designed for MS Windows, which is my target platform – by another OS I actually meant another version of MS Windows: XP -> Win7. The idea is to get address space of the loaded module (.exe or .dll) and check if block is within this address space. Code which calculates start/end of static area is put into ‘lib’ segment thus it should be executed before all other static objects from ‘user’ segment, i.e. constructor can assume that staticStart/End variables are already initialized.

    #include <psapi.h>
    
    #pragma warning(push)
    #pragma warning(disable: 4073)
    #pragma init_seg(compiler)
    #pragma warning(pop)
    
    HANDLE gDllHandle = (HANDLE)-1;
    LONG_PTR staticStart = 0;
    LONG_PTR staticEnd = 0;
    
    struct StaticAreaLocator {
        StaticAreaLocator() {
            if( gDllHandle == (HANDLE)-1 )
                gDllHandle = GetModuleHandle(NULL);
    
            MODULEINFO  mi;
            GetModuleInformation(GetCurrentProcess(), (HMODULE)gDllHandle, &mi, sizeof(mi));
    
            staticStart = (LONG_PTR)mi.lpBaseOfDll;
            staticEnd = (LONG_PTR)mi.lpBaseOfDll + mi.SizeOfImage;
    
            // ASSERT will fail in DLL code if gDllHandle not initialized properly
            LONG_PTR current_address;
            #if _WIN64
                ASSERT(FALSE) // to be adopted later
            #else
                __asm {
                            call _here
                    _here:  pop eax                     ; eax now holds the [EIP]
                            mov [current_address], eax
                }
            #endif
            ASSERT((staticStart <= current_address) && (current_address < staticEnd));
            atexit(cleanup);
        }
    
        static void cleanup();
    };
    
    StaticAreaLocator* staticAreaLocator = new StaticAreaLocator();
    
    void StaticAreaLocator::cleanup() {
        delete staticAreaLocator;
        staticAreaLocator = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( Preface: This is my first audio-related question on Stack Overflow, so I'll try
Let me preface this question by saying I've exhausted Google, or at least what
I should preface this question by saying that I am new to JavaServer Pages.
Let me preface this question. I have just started using jquery, so please be
Ok, let me preface this question with the fact that I am extremely new
As a preface to this question: I am brand new to Rails development (and
Let me preface by saying that I saw this other question on the subject
Firstly, I know this [type of] question is frequently asked, so let me preface
To preface this question: I've never actually done iPhone development. Haven't even downloaded the
First let me preface this question by saying that I'm fairly new to Javascript.

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.