0

Update: I think I was able to narrow down the problem, see here for a new question that is hopefully more precise.

Update 2: I was able to solve the problem, see the link above :-)

I am trying to understand whether I got something fundamentally confused about how static member variables work.

I have a class (Lets call it cProvider) that contains static member variables (e.g. a pointer) and get/set methods. This class is included by two other classes, let's call them "cWriter" and "cReader", and both instantiate it. cWriter only accesses the setter methods, cReader accesses the getter methods.

My problem is that seem to be multiple instances of the static variables, meaning that when I access a static variable of cProvider through cWriter, it accesses a different physical location than when I access the same variable through cReader.

Is this something that is to be expected? I am doing this in a rather complex and probably unknown framework, ADTF. It might well be that the framework is responsible for this behavior. I checked the process IDs, and cWriter and cReader have the same process ID.

What could be the reason that they still access different addresses? I never had a formal programming education, so I might be missing something fundamental about how static variables work. I am happy for any hints about what could be the problem!

Edit: Condensed version of my code: (To show the gist)

OdometryHistoryWriter.h:

class cOdometryHistoryWriter
{
        static bool something;
}

OdometryHistoryWriter.cpp:

bool cOdometryHistoryWriter::something;

OdometryHistoryProviderInputFilter.h:

#include OdometryHistoryWriter.h

class cOdometryHistoryProviderInputFilter
{
    cOdometryHistoryWriter m_odoHistWriter;
    void setSomething(boolvar);
}

OdometryHistoryProviderInputFilter.cpp:

#include OdometryHistoryProviderInputFilter.h

void OdometryHistoryProviderInputFilter::setSomething(boolvar)
{
    m_odoHistWriter.something = boolvar;
    return;
}

OdometryHistoryProvider.h:

class cOdometryHistoryProvider
{
    bool getSomething();
    cOdometryHistoryWriter m_odoHistAcessor;
}

OdometryHistoryProvider.cpp:

bool  cOdometryHistoryProvider::getSomething()
    { return m_odoHistAcessor.something;}
Community
  • 1
  • 1
Thomas
  • 4,696
  • 5
  • 36
  • 71
  • 2
    Please edit your question with code for the example classes. – Thomas Matthews Mar 25 '15 at 00:16
  • So far I never actually had to post to SO, the search always answered my questions. If my question is unclear, please ask and I'll try to provide addtional detail – Thomas Mar 25 '15 at 00:16
  • 1
    You're mentioning PIDs — are you trying to establish communication between two distinct processes ? – Quentin Mar 25 '15 at 00:20
  • 1
    Without some code to show us what you are trying to do (ideally simplified into as few lines as is necessary for the example to compile), it's very hard to understand exactly what is going on (unless Quentin is right, and you are looking at two different processes, in which case, yes, they would be two different static members) – Mats Petersson Mar 25 '15 at 00:25
  • 1
    is `bool cOdometryHistoryWriter::something;` line in .cpp or in an .h file? – Anton Savin Mar 25 '15 at 00:26
  • @Quentin: No, this was to check whether they run in the same process. ADTF (the framework my code is running in) is pretty complex, so I wanted to make sure that the parts of my code don't actually run in different processes for some reason (they don't) – Thomas Mar 25 '15 at 00:27
  • @ Anton Savin: This is in a c++ file. Should I partition my code example by files? – Thomas Mar 25 '15 at 00:28
  • 1
    @Thomas yes, better to do it – Anton Savin Mar 25 '15 at 00:29
  • Also, I guess my main question would be: What would be the way to have different static members running in the same process? Because this is what's happening, and I was hoping for any hints on what to look for... I don't expect anybody to debug my code for me, I just need a hint to know what I should look for :-) – Thomas Mar 25 '15 at 00:31
  • 1
    [See here](http://stackoverflow.com/help/mcve) for how to constructively post a code sample. If multiple files are required that's fine . – M.M Mar 25 '15 at 00:34
  • Thanks so far everybody! I realise now that this might be a bit too complex / specific for SO. I'll ask somebody who knows a lot more about the framework I am working in tomorrow and update my question if I have a more specific guess about what part of my code might be responsible. I'll also post an update if I found what was wrong, especially if it wasn't ADTF specific and might help somebody else in the future. – Thomas Mar 25 '15 at 00:54

1 Answers1

4

Not really an answer, but it's far too long to make a comment, and code in comments is hopeless to read even when it fits [unless it's really short]

I just did this based on your code:

#include <iostream>

class cOdometryHistoryWriter
{
public:
    static bool something;
};

bool cOdometryHistoryWriter::something = false;


class cOdometryHistoryProviderInputFilter
{
public:
    cOdometryHistoryWriter m_odoHistWriter;
    void setSomething(bool b) { m_odoHistWriter.something = b; }
};

class cOdometryHistoryProvider
{
public:
    bool getSomething() { return m_odoHistAcessor.something; }
    cOdometryHistoryWriter m_odoHistAcessor;
};


int main()
{
    cOdometryHistoryProvider a;
    cOdometryHistoryProviderInputFilter b;

    b.setSomething(true);
    std::cout << "Expect true:" << a.getSomething() << std::endl;
    b.setSomething(false);
    std::cout << "Expect False:" << a.getSomething() << std::endl;
}

and it outputs:

Expect true:1
Expect False:0

as you'd expect (at least I do).

As long as you only have ONE definition of bool cOdometryHistoryWriter::something = false;, it should only ever have one address, and be accessible as one and the same everywhere. If this is not happening, the there's SOMETHING different between your ACTUAL code and the code you posted (not that unusual, I expect more than half of the questions I look at are missing "the code that actually make it go wrong" [inclusive cases of "no code posted"])

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Hi Mats, thanks for trying that. This is exactly how my code was supposed to behave. I realise now that my question might be too complex for SO, since the error might well be in my understanding of how the framework I am working in handles code. I probably should look more into that. – Thomas Mar 25 '15 at 00:50
  • 1
    Maybe you can take you actual framework and "pick it apart into small pieces". – Mats Petersson Mar 25 '15 at 08:48