0

I've tried to create a minimal example of my problem. I'm trying to check if the address of an void pointer is NULL or not. The address should be overgiven by constructing the class, and should be const. I wrote the class below.

MyPointer.h:

public:
    MyPointer(const void* activeApp) : m_activeApp(activeApp){
        std::cout <<"adresse on construction: " << m_activeApp << std::endl;
    };
MyPointer.cpp:
    void printAdress();
private:
    const void* m_activeApp;
};

The Methode "printAdress" should be able to print the correct address of the given pointer.

int main(void){
   void* p_activeApp = nullptr;
   std::cout << p_activeApp << std::endl;

   MyPointer myPointer(p_activeApp);
   std::cout << "Should be 0: " ;
   myPointer.printAddress();

   p_activeApp = new(bool);

   std::cout << "Should be anything: ";
   myPointer.printAddress();
}

void MyPointer::printAddress() {
   std::cout << this->m_activeApp << std::endl;
};

Of course it doesn't work, because the m_activeApp still points to NULL, but how can I change this?

Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
  • For one thing, your question's wording isn't clear: Do you mean the address of the pointer (the location where the pointer object is in memory) or the address that the pointer holds (the location that the pointer points to)? BTW: Concerning a [mcve], you aren't even close, because the code is far away from being compilable. As a new user, please also take the [tour] and read [ask]. – Ulrich Eckhardt Jan 02 '20 at 13:24
  • 1
    Why would the value of the member change when you assign to a different variable? (The most important thing to learn about pointers is that there is nothing special about pointers.) – molbdnilo Jan 02 '20 at 13:27
  • It looks like your are expecting the change of `p_activeApp` to also have an effect on `m_activeApp` which is not the case. They are two separate values. – super Jan 02 '20 at 13:27

3 Answers3

2

If you want to change what myPointer.m_activeApp points to, you have to set the pointer to a different value, simple as that. This pointer and p_activeApp are two distinct, independent pointers. Changing one does not change the other.

What you can do is to make one pointer a reference to the other pointer instead. Then, changing one would also change the other. This will work, though be warned, it won't be good programming style.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • *it won't be good programming style* I'd say it's more like a "really, really, really BAD programming style" to have a class member variable as a reference to some outside parameter as it's almost certainly going to lead to undefined behavior along the lines of https://stackoverflow.com/questions/9941502/c-reference-to-out-of-scope-object – Andrew Henle Jan 02 '20 at 13:36
  • True, @AndrewHenle. However, here's an example where this does make sense: https://en.cppreference.com/w/cpp/thread/lock_guard. In short, I'd advise to learn it and to learn when not to use it. – Ulrich Eckhardt Jan 02 '20 at 13:41
  • ``` MyPointer(const void* activeApp) : m_activeApp(&activeApp) ``` like this? – roalmang Jan 02 '20 at 13:47
  • This seems only to return the adresse were the pointer is stored – roalmang Jan 02 '20 at 13:49
  • No. Please read the section about references in your C++ tutorial again. Then, try to achieve something like this using an `int` and a reference to it. If you have that working, substitute the `int` with `void*` to get it to work with pointers. I'd suggest these two steps, because the syntax with the `&` and `*` gets messy for a beginner. – Ulrich Eckhardt Jan 02 '20 at 13:53
  • Thanks for your help. I found a way to fix it with double pointes, its below in the answers. – roalmang Jan 02 '20 at 15:41
0

Pointers are integers (representing a memory location), so imagine void* to be a number. You set p_activeApp to 0, then you construct a MyPointer, and its internal pointer is set to 0.

Then you change the first number by using the new operator, p_activeApp now gets a value (to that new memory address), but there's no reason for m_activeApp to also change. It still points at 0, no matter what p_activeApp changes to.

Bas in het Veld
  • 1,271
  • 10
  • 17
0

I fixed it like this:

public:
    MyPointer(void** activeApp) : m_pp_activeApp(activeApp) {
        std::cout <<"address on construction: " << *m_pp_activeApp << std::endl;
    };


    void printAddress();
private:
    void** m_pp_activeApp;
};
int main(void){
    void* p_activeApp = nullptr;
    void** pp_activeApp = &p_activeApp;

    MyPointer myPointer(pp_activeApp);

    std::cout << "Should be 0: " ;
    myPointer.printAddress();

    p_activeApp = new(bool);

    std::cout << "Should be anything: ";
    myPointer.printAddress();

}

void MyPointer::printAddress() {
    std::cout << *m_pp_activeApp << std::endl;
};