Carles Ràfols
Carles Ràfols

Reputation: 87

Class member changing address after constructor (thread)

I am trying to store the class member of class A as a const reference in class B (which is controlled by A) so that I can modify the value only through class A but B can still read it. The problem is that the address of the class A members change after the constructor finishes so the reference members in class B won't point to the class A members anymore.

Each object A runs in a separate thread. I don't get different addresses when I don't use multithreading so I know that's what's causing the problem but I can't figure out what's going wrong.

#include <thread>
#include <vector>
#include <iostream>

using namespace std;

class B
{
private:
    const float& m_foo;
public:
    friend class A;
    B(const float& foo):
        m_foo(foo)
    {
        cout << "m_b.m_foo address in B constructor:\t" << &m_foo << endl;
    }
};

class A {
private:
    float m_foo;
    B m_b; 

public:
    A(float foo):
        m_foo(foo),
        m_b(m_foo)
    {
        cout << "m_a.m_foo address in A constructor:\t" << &m_foo << endl;
        cout << "m_b.m_foo address in A constructor:\t" << &(m_b.m_foo) << endl;
    }
    void run()
    {
        cout << "m_a.m_foo address after A and B constructor:\t" << &m_foo << endl;
        cout << "m_b.m_foo address after A and B constructor:\t" << &(m_b.m_foo) << endl;
    }
};

int main(int argc, char *argv[]){

        int n = 1;
        vector<A> vector_A;
        vector<thread> threads;
        vector_A.reserve(n);

        for (int i=0; i<n; i++){
            // Store A objects inside vector_A
            vector_A.push_back(A(10));
            // Store the threads inside the threads vector
            threads.push_back(thread(&A::run,
                std::ref(vector_A[i])));}
        for (int i=0; i<n; i++){
            // Wait for the rest of the thread to finish
            threads[i].join();
        }

    return 0;
}

Output:

m_b.m_foo address in B constructor:     0x7ffee160d708
m_a.m_foo address in A constructor:     0x7ffee160d708
m_b.m_foo address in A constructor:     0x7ffee160d708
m_a.m_foo address after A and B constructor:    0x7faab8c02af0
m_b.m_foo address after A and B constructor:    0x7ffee160d708

Upvotes: 1

Views: 314

Answers (1)

Tom Kim
Tom Kim

Reputation: 436

You can use something like this: (it works for me)

  • add function which init A objects and call run:
void newA_run(vector<A>& v, float val){
    // Store A objects inside vector_A
    A a(val);
    v.push_back(a);

    a.run();
}
  • edit the main like this:
int main(int argc, char *argv[]){

        int n = 1;
        vector<A> vector_A;
        vector<thread> threads;
        vector_A.reserve(n);

        for (int i=0; i<n; i++){
            // Store the threads inside the threads vector
            //threads.push_back(thread(&A::run, std::ref(vector_A[i])));}
            threads.push_back(thread(&newA_run,
                                     std::ref(vector_A),
                                     10));
        }
        for (int i=0; i<n; i++){
            // Wait for the rest of the thread to finish
            threads[i].join();
        }

    return 0;
}

Update:

There is a easier way:

  • Edit only vector_A in main as follows:
int main(int argc, char *argv[]){

    int n = 1;
    A* vector_A[n];// = (A*) malloc(n * sizeof(A));
    vector<thread> threads;

    for (int i=0; i<n; i++){
        // Store A objects inside vector_A
        vector_A[i] = new A(10);
        // Store the threads inside the threads vector
        threads.push_back(thread(&A::run,
            std::ref( *vector_A[i] )));}
    for (int i=0; i<n; i++){
        // Wait for the rest of the thread to finish
        threads[i].join();

        // I put public 'm_foo' in both classes and 'm_b', to check after join
        // and it works fine.
        cout << "m_a.m_foo address after join:\t" << &(vector_A[i]->m_foo) << endl;
        cout << "m_b.m_foo address after join:\t" << &(vector_A[i]->m_b.m_foo) << endl;
    }

    return 0;
}

Hope it helps you.

Upvotes: 2

Related Questions