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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:54:33+00:00 2026-06-09T03:54:33+00:00

I have a question regarding static variables, or some other way to do so.

  • 0

I have a question regarding static variables, or some other way to do so.

I have a master class, PatternMatcher. I have several derived units from that, depending on what matcher is used. Now each subclass needs to store a vector of floats, but within each class it is constant. The data for that vector is read during initialization, and can be up to 1GB in size (smallest I have is 1MB, biggest is 1GB).
Currently when I have for example two instances of Matcher_A, it would allocate twice the memory. I do not know in advance which matchers are to be used (per run it will be three matchers, and you can use the same matcher several times). I would prefer to not check during run time whether the wanted matcher is already initialized somewhere, since this would require additional code for every change I do.

Currently I allocate the 3 matchers with

PatternMatcher* a = new PMMatcherA();
PatternMatcher* b = new PMMatcherB();
PatternMatcher* c = new PMMatcherC();

, but since they are user selected, it could happen that A and C are the same for example. When I run a check via typeid(a).name();, it would give me PatternMatcher as result, never matter what class I used to initiate with. PatternMatcher basically is purely a virtual class.

I always thought that static means that a variable is constant over different allocations, but when I define my vector as static, I would get a linker resolve error. In an earlier iteration, I had these vectors global, but would prefer them to be localized to their classes.

What are the keywords I need to use to have the vector from on initialization be available for the next initialization already? A simple check if the vector size is greater than 0 would already be enough, but every object uses its own vector.

  • 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-09T03:54:34+00:00Added an answer on June 9, 2026 at 3:54 am

    static keyword is a way to go – that would store exactly one copy of a member for the whole class. What you were missing is an actual declaration of such static in a compilation module so that the linker can use it. For instance:

    header file foo.h:

    struct Foo {
      static int s_int;
    }
    

    source file foo.cpp:

    #include "foo.h"
    int Foo::s_int; // optionally =0 for initialization
    

    The second part is vital as this will allocate a memory space for the object to be used as a static member.

    Keep in mind, though, that:

    • static members will all be initialized before the main(), which means your 1GB of data will be read regardless of whether anyone ever uses that particular class
    • You can work around the abovementioned issue, but then you will have to be checking if the data load and initialization has happened during run-time

    There’s another option for you, however. If you store your floats “as-is” (i.e. 32 bits per each, in binary format) you can just simply “map” the files into memory spaces and access them as if they were already loaded – the OS will take care of loading appropriate 4K pages into RAM when needed.

    Read more about mmap at http://en.wikipedia.org/wiki/Mmap

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

Sidebar

Related Questions

Question regarding static variables in static classes. If i have a static class and
I have a question regarding static function in php. let's assume that I have
I have question regarding the SQLAlchemy. How can I add into my mapped class
i have a question regarding the AsyncTask class in android, and why it is
I have a question regarding the proper way to modify a php DateTime object.
I have a question regarding class design. I want to have a class that
I have a question regarding static members and functions of non static classes. I
I have a question regarding the FractionRgbData class of Neuroph ImageRecognition of Neuroph 2.6.
I got some question regarding the shared and static libraries.So let me tell you
Yet another static question. I have read the following: What are static variables? file

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.