-5

maybe this question will be a bit complicated and maybe i'm missing something of stupid. I'll try to explain without any source code, because my project is big and i don't know how/where to start. I have:

bool result = false;
bool* pointer = &result;

these variables are stored in some classes.. (not as in the above code). When result is created, his address is something like 0x28fddc. and the pointer variable takes this address. Suddendly, without any reason (maybe), his address is not 0x28fddc anymore but something like 0x3a8c6e4. With the pointer variable, i am trying to change the result var by doing:

*result = true;

But obviously, this doesn't work (and it doesn't give me any error). It will not change result var because it's in another address. I don't know why this happens. Can you only tell me how could this happen? And i'll try to fix. (This classes are everytime being updated in some functions with parameters passed by reference). For example:

void update_class(obj_class &obj);

(These names are only an example). I hope i've been clear and if not, i'll just delete this topic. Sorry for bad english but i'm italian :)

EDIT: Now i'll try to provide some code..

button.h

class button
{
public:
    void check_tap(SDL_Event* e);
    bool* done;
}

messagebox.h:

class messagebox 
{
public:
    messagebox();
    bool result_ok;
    button btn_ok;
}
void check_tap(std::vector<messagebox> &msgbox, SDL_Event* e) {
    for(unsigned int k=0; k<msgbox.size(); k++) {
        msgbox[k].btn_ok.check_tap(e);
        // check_tap is a function that i create for checking if the user is tapping the button with his finger or not. When the user presses the button and leaves it the done variable should become true, but result_ok seems not to be affected because his address here is different. This problem is only in this case using messagebox. I created more other buttons outside and all works perfect.
    }

}

messagebox.cpp:

messagebox::messagebox() {
    // Initializing things
    btn_ok.done = &result_ok;
    // Here, btn_ok.done gets the address of result_ok..
}

main.cpp:

std::vector<messagebox> msgbox;
msgbox.push_back(msgbox());
manudicri
  • 176
  • 14

1 Answers1

3

No, the address of variables do not change during their lifetime.

However, storing the address of a variable is problematical if the variable ceases to exist. A simple example is

 #include <iostream>
 int *p;

 void f()
 {
     int i;
     p = &i;
 }

 int main();
 {
     f();
     std::cout << (void *)p << '\n';

     //    other code here

     f();
     std::cout << (void *)p << '\n';
 }

In the above case, the two values of p may be the same, or they may differ. This is not because the address of variables change. It is because a variable, i is created each time f() is called, and ceases to exist when f() returns. The variable i in the first call of f() is, as far as your program is concerned, a distinct variable from i during the second call of f().

Depending on what happens with "other code here" in the above, the memory occupied by i in the first call of f() may be inaccessible (e.g. used for another variable) during the second call of f() - so, during the second call of f(), i will have a different address than during the first. There are no guarantees - you may also get lucky (or unlucky depending on how you look at it) and the addresses printed will be the same.

If you are getting behaviour that suggests, to you, that the address of a variable is changing then - somewhere in your code - there is a bug of some form. Typically, this will involve storing the address of a variable in a pointer, and using (or accessing the value of) the pointer after the variable ceases to exist. And any dereferrencing of that pointer (e.g. to access the variable pointed at) has undefined behaviour.

For example, any usage of *p like

 *p = 42;

or

 std::cout << *p << '\n';

in the main() I have given above will give undefined behaviour.

The act of assigning a pointer to contain the address of that variable does not change the lifetime of that variable.

Peter
  • 35,646
  • 4
  • 32
  • 74