Unexpected pointer action

Discussion in 'C++' started by jvh, Dec 30, 2011.

  1. jvh

    jvh Guest

    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
     
    jvh, Dec 30, 2011
    #1
    1. Advertising

  2. jvh

    LR Guest

    jvh wrote:
    > 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();
    }
     
    LR, Dec 30, 2011
    #2
    1. Advertising

  3. jvh

    jvh Guest


    >
    > 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)
     
    jvh, Dec 30, 2011
    #3
  4. jvh

    MikeWhy Guest

    jvh wrote:
    >> 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.


    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);
    ...
    }
     
    MikeWhy, Dec 30, 2011
    #4
  5. jvh

    gwowen Guest

    On Dec 30 2011, 7:31 pm, jvh <> wrote:
    > 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();
    }
     
    gwowen, Jan 4, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Bloggs
    Replies:
    1
    Views:
    994
    Sudsy
    Aug 3, 2003
  2. rjweytens
    Replies:
    6
    Views:
    16,358
    rjweytens
    Jun 25, 2004
  3. runescience

    Struts mapping action to action???

    runescience, Feb 6, 2006, in forum: Java
    Replies:
    3
    Views:
    1,955
    runescience
    Feb 7, 2006
  4. vyshu
    Replies:
    1
    Views:
    2,706
    Richard Senior
    Apr 27, 2007
  5. John
    Replies:
    0
    Views:
    1,011
Loading...

Share This Page