Unexpected pointer action

J

jvh

A friend told me that he had read somewhere that giving pointers to an
average programmer is like giving a gun to a 6 year old. Since I got
the wrong answer for the problem below I must be an average
programmer.

I got this problem from cprogramming.com. Does it display 10 or 12 and
why?

void afunction(int *x)
{
x = new int;
*x = 12;
}

void main()
{
int v = 10;
afunction(&v);
cout << "\nv:" << v;

getchar();
}


Thanks,
jvh
 
L

LR

jvh said:
void afunction(int *x)
{
x = new int;
*x = 12;
}

void main()

int main()
{
int v = 10;
afunction(&v);
cout << "\nv:" << v;

std::cout << "\nv:" << v;
getchar();
}

It displays 10. C++ passes the address of v in main to afunction by
value. The parameter, x, in afunction is the value of the pointer, and
changing it doesn't change v in main.


I think it'll become clearer if you try adding some output to see the
changing values, maybe like so:

void a(int *x) {
std::cout << "a,x " << x << " a,*x " << *x << std::endl;
x = new int(12); // leaks memory
std::cout << "a,x " << x << " a,*x " << *x << std::endl;
}

void testa() {
int v = 10;
std::cout << "testa,&v " << &v << " testa,v " << v << std::endl;
a(&v);
std::cout << "testa,&v " << &v << " testa,v " << v << std::endl;
}

int main() {
testa();
}
 
J

jvh

It displays 10.  C++ passes the address of  v in main to afunction by
value.  The parameter, x, in afunction is the value of the pointer, and
changing it doesn't change v in main.

I think it'll become clearer if you try adding some output to see the
changing values, maybe like so:

void a(int *x) {
    std::cout << "a,x " << x << " a,*x " << *x << std::endl;
    x = new int(12); // leaks memory
    std::cout << "a,x " << x << " a,*x " << *x << std::endl;

}

void testa() {
    int v = 10;
    std::cout << "testa,&v " << &v  << " testa,v  " << v << std::endl;
    a(&v);
    std::cout << "testa,&v " << &v  << " testa,v  " << v << std::endl;

}

int main() {
    testa();

Thanks for the post, I should know better. If x had not been
reassigned then it would display 12. So I think it would be correct to
say that V was passed by reference (by its address) but it's address
was passed by value.

I had put it many display statements and they were telling me what you
told me but not as concisely and I did not see it.

Thanks again,

jvh (AKA an average programmer)
 
M

MikeWhy

jvh said:
Thanks for the post, I should know better. If x had not been
reassigned then it would display 12. So I think it would be correct to
say that V was passed by reference (by its address) but it's address
was passed by value.

That's still not right. The following is closer to equivalent:

void a(int * p)
{
int * x = new int;
*x = 12;
}

You probably intended the following instead:

void a(int ** p)
{
*p = new int;
**p = 12;
}

int main()
{
int * p;
a(&p);
...
}
 
G

gwowen

A friend told me that he had read somewhere that giving pointers to an
average programmer is like giving a gun to a 6 year old. Since I got
the wrong answer for the problem below I must be an average
programmer.

I got this problem from cprogramming.com. Does it display 10 or 12 and
why?

void afunction(int *x)
{
        x = new int;
        *x = 12;

}

void main()
{
        int v = 10;
        afunction(&v);
        cout << "\nv:" << v;

        getchar();

}

In addition to what's been said, its sometimes illuminating to
"inline" the function yourself, so you can see what's going on.

void main()
{
int v = 10;
// afunction(&v);
{
int *x = &v; // x points to v
x = new int; // x now points someplace else
*x = 12; // set the value stored "someplace else"
}
cout << "\nv:" << v;
getchar();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top