0

I have a question about the following code. I am not sure why the address stored in yAdd doesn't get updated to the same address stored in the variable x inside the addfun() function in the code below? what is wrong with my thinking?

#include <iostream>

using namespace std;

void fun(int *x) {
    *x = 55;
}

void addfun(int **x) {
    *x = new int[3];
    cout << x << endl;  // x stores the address of int[0]
                        // say it is 0x7ffc32e646a0
    cout << &x << endl;
}

int main()
{
    int y = 99;
    int *yAdd = &y;
    cout << y << endl;
    fun(yAdd);
    cout << y   << endl;
    cout << yAdd << endl; // original address stored in yAdd
    cout << "address of y is " << &y <<  endl;
    addfun(&yAdd);   // so I update the address stored in yAdd to be the same address
                    // as stored inside the x in the addfun function
    cout << yAdd << endl; // but how come when printing this, yAdd doesn't 
                    // show 0x7ffc32e646a0, but something else?
    return 0;
}

3 Answers 3

1
addfun(&yAdd); 

This is passing the address of yAdd to this function, as a parameter.

yAdd is a pointer. The pointer is an address of some other object in your program. It doesn't matter what that object is, yAdd is its pointer, and whatever yAdd itself, its address gets passed.

cout << yAdd << endl; 

This shows the value of yAdd. Whatever this pointer is, its value is shown. And the address of this object, of yAdd, is something completely different.

void addfun(int **x)

x is a parameter to this function. x is a variable, that has nothing to do, whatsoever, with any other variable in your program. As such, it will have its own memory address, different than the memory address of everything else in your program (at least while it exists).

cout << &x << endl;

And this shows x's address. It is not showing the value of yAdd. It is not showing yAdds address, either. It is showing the address of x, a parameter to this function.

All of these are separate, distinct, objects and values in your program.

Sign up to request clarification or add additional context in comments.

2 Comments

oh I don't mean cout << x << endl; should show address of yAdd. I mean when I put the address of yAdd into the addfun function, should that function not modify what pointed to by yAdd? So shouldn't yAdd now pointed to the same place as pointed to by the x variable inside the addfun function? i.e. shouldn't they point the same place? i.e. yAdd == x ?
No it's not. x is a parameter to addfun(). When x returns, it gets destroyed. It ceases to exist. It starts pining for the fjords. It is no more. It is an ex object. yAdd is not x. yAdd is what *x used to be. yadd() does not set x. It literally sets *x. yAdd set *x. It does not sets x. That's what its code literally says. Printing what x is, or what it's address is, has absolutely nothing to do, whatsoever, with what *x is.
1

I changed the code to print out some more and label it better:

#include <iostream>

using namespace std;

void fun(int *x) {
    *x = 55;
}

void addfun(int **x) {
    cout << endl << "addfun()" << endl;
    cout << "x  = " <<  x << endl;
    cout << "*x = " << *x << endl;
    cout << "new int[3]" << endl;
    *x = new int[3];
    cout << "x  = " <<  x << endl;
    cout << "*x = " << *x << endl;
    cout << "addfun() done" << endl << endl;
}

int main()
{
    int y = 99;
    int *yAdd = &y;
    cout << y << endl;
    fun(yAdd);
    cout << y   << endl;
    cout << "&yAdd = " << &yAdd << endl;
    cout << "yAdd  = " << yAdd << endl;
    cout << "*yAdd = " << *yAdd << endl;
    cout << "address of y is " << &y <<  endl;
    addfun(&yAdd);   // so I update the address stored in yAdd to be the same address
                    // as stored inside the x in the addfun function
    cout << "&yAdd = " << &yAdd << endl;
    cout << "yAdd  = " << yAdd << endl;
    cout << "*yAdd = " << *yAdd << endl;
    return 0;
}

This produces for example the following output:

99
55
&yAdd = 0x7ffc9e47fe58
yAdd  = 0x7ffc9e47fe54
*yAdd = 55
address of y is 0x7ffc9e47fe54

addfun()
x  = 0x7ffc9e47fe58
*x = 0x7ffc9e47fe54
new int[3]
x  = 0x7ffc9e47fe58
*x = 0x564ab462d2c0
addfun() done

&yAdd = 0x7ffc9e47fe58
yAdd  = 0x564ab462d2c0
*yAdd = 0

As you can see when you enter addfun the value of x is the address of yAdd which points to y. Then you allocate the int[3] and assign the result to *x. That is you store the address of int[3] into where x points to, namely yAdd. The value of x is not changed.

After the call to addfun you can see that the address of yAdd is still the same as and the same as x was but where yAdd points to has now been changed to int[3].

I think the bit where you got confused is *x = .... That changes the things x points at and not x itself.

1 Comment

Oh, I think I know where my confusion is now. Thanks. But I am just wondering so now yAdd points to the array of size of 3 integers. So is there anyway I can use yAdd to put some elements into the 3 positions of that array I allocated? like can I do yAdd[0] = 987, yAdd[1]= 75, yAdd[2] = -13 ? like can I treat yAdd as an array and use the above notation to assign values into the specific positions? and similarly can I use *yAdd = 987, *(yAdd + 1) = 75, *(yAdd + 2) = -13?
0

Within the function addfun

void addfun(int **x) {
    *x = new int[3];
    cout << x << endl;  // x stores the address of int[0]
                        // say it is 0x7ffc32e646a0
    cout << &x << endl;
}

the statement

    cout << x << endl;  // x stores the address of int[0]
                        // say it is 0x7ffc32e646a0

prints the address of the original pointer yAdd passed to the function

addfun(&yAdd);

In main this statement

cout << yAdd << endl;

outputs the value assigned to the pointer within the function addfun in this statement

    *x = new int[3];

that is the address of the dynamically allocated memory.

The assigned value to the pointer yAdd and the address of the pointer itself are two different values.

2 Comments

But shouldn't yAdd now stores the same address as what x storing now? i.e. these two pointers both point to the same place in memory?
No, x points to yAdd and you store the allocated array into where x points to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.