Pointers with the same adress

Hi there,

I have a function to which I pass a double**, which I then swap with another double** in this function, however it seems there are some complications.

inside main I pass this as an argument to a function (declared outside main):

main{

double** B = allocDouble(x,y);

function(double** B){

static double** tmp = allocDouble(x,y);
std::swap(B,tmp);
fprintf(stderr,"B: %p\n",B); // one address

}

fprintf(stderr,"B: %p\n",B); // another adress

}

Then when I print the address of B via fprintf(stderr,"B: %p\n",B), inside the function, I do not get the same address as outside the function. Where I instead get the address of tmp.


So it seems there is an issue with B inside the function, being a copy of B outside the function or in the main.

So why is this? And what can I do about it, so it is the actual B that is changed?



Then when I print the address of B

No, you print the value of B.

Swap does not change the pointed-to values of B and tmp. It swaps their values; the values of the copy of B inside your function.



Hi and thanks for you reply!

How do I then swap the value of not the copy of B, but the actual B outside my function.

And when I print either B or tmp I get something like:

0x1cf18e40

How is that not the address of B or tmp?
How do I then swap the value of not the copy of B, but the actual B outside my function.

Pass B by reference.

function(double** B)

becomes function(double** & B)
Hi and once thanks! :)

Yes I also kind of thought about doing that.

It is correctly understood that:

If you just want to manipulate values of pointer - matrix entries just pass pointer, but if you want to manipulate pointer itself, pass reference - like when swapping.

And also how would I declare B, so that it is a reference?
Because if I pass B to the function with double** & B, I get an error.
And also how would I declare B, so that it is a reference?

Nothing else should need to be changed, apart from the function header.
Hi,

Thanks a lot, it seems to make it work.

So is this good C++ programming practice or something that should be avoided?

And if the other pointer you swap B with is static, the other pointer should keep its newly assigned address right?
So is this good C++ programming practice or something that should be avoided?
Hard to say. It seemed like this was just an example for learning purposes. Learning how things work is good.

And if the other pointer you swap B with is static, the other pointer should keep its newly assigned address right?
I'm not quite clear on the question here. Perhaps you could show some actual code to clarify what you mean. (you might also test it yourself to see if it behaves as expected).

Well it would look like this:

main{

double** B = allocDouble(x,y);

function(double** & B){

static double** tmp = allocDouble(x,y);
std::swap(B,tmp);

}

}

And then tmp should keep its new address right?
Roughly speaking yes. tmp will retain its value from one function call to the next.

However, these code samples don't compile, so the whole discussion feels very abstract. Trying to understand the C++ language while looking at invalid code doesn't seem a very sound plan to me. I suggest actually trying to compile and run some examples, and examine the contents of different variables (using a debugger is a good idea) during execution of the code.
Topic archived. No new replies allowed.