Why pointers?

Discussion in 'C++' started by saneman, Feb 13, 2008.

  1. saneman

    saneman Guest

    I have written C++ code for some time now using both pointers and
    references.

    I was asked what the point was in using pointers and could not give a
    short an explanatory example.

    I would say that you use a pointer when data are updated across function
    calls.

    But does anyone have an idea to a minimal example where its impossible
    to avoid pointer?
    saneman, Feb 13, 2008
    #1
    1. Advertising

  2. saneman

    Guest

    , Feb 13, 2008
    #2
    1. Advertising

  3. saneman

    saneman Guest

    wrote:
    > [8.6] When should I use references, and when should I use pointers?
    > http://www.parashift.com/c -faq-lite/references.html#faq-8.6


    Thanks but I was looking for some code sample that illustrates the
    differences. In the below code 'test' takes a pointer to a list. But I
    never use 'new' to make it work. Why would it ever be necessary to use
    'new' when I can just pass the address to the list to 'test' using '&'?


    #include<iostream>
    #include<list>
    void test(std::list<int>* l) {
    l->push_back(555);
    }

    int main() {

    std::list<int> l;
    std::list<int>::iterator list_it;
    l.push_back(1);
    l.push_back(2);
    l.push_back(3);

    for (list_it = l.begin(); list_it != l.end(); list_it++) {
    printf("%d\n",*list_it);
    }
    std::cout<<std::endl;
    test(&l);
    for (list_it = l.begin(); list_it != l.end(); list_it++) {
    printf("%d\n",*list_it);
    }
    return 0;
    }
    saneman, Feb 13, 2008
    #3
  4. On Feb 13, 10:57 pm, saneman <> wrote:

    > But does anyone have an idea to a minimal example where its impossible
    > to avoid pointer?


    Think about polymorphism in C++.

    Best Regards,
    Szabolcs
    Szabolcs Ferenczi, Feb 13, 2008
    #4
  5. On 2008-02-13 22:57, saneman wrote:
    > I have written C++ code for some time now using both pointers and
    > references.
    >
    > I was asked what the point was in using pointers and could not give a
    > short an explanatory example.
    >
    > I would say that you use a pointer when data are updated across function
    > calls.
    >
    > But does anyone have an idea to a minimal example where its impossible
    > to avoid pointer?


    In any situation where you need to be able to "reseat" the reference. On
    example would be walking through the nodes in a linked list without
    using recursion, something like:

    node* n;
    while (n != 0)
    {
    // do something with n
    n = n->next;
    }

    --
    Erik Wikström
    Erik Wikström, Feb 13, 2008
    #5
  6. saneman

    Daniel T. Guest

    saneman <> wrote:
    > wrote:


    > > [8.6] When should I use references, and when should I use pointers?
    > > http://www.parashift.com/c -faq-lite/references.html#faq-8.6

    >
    > Thanks but I was looking for some code sample that illustrates the
    > differences. In the below code 'test' takes a pointer to a list. But I
    > never use 'new' to make it work. Why would it ever be necessary to use
    > 'new' when I can just pass the address to the list to 'test' using '&'?


    That's a change of subject. First you ask "why pointers?", now you are
    asking "why new?".

    What is it that you really want to ask?
    Daniel T., Feb 14, 2008
    #6
  7. saneman

    Jeff Schwab Guest

    saneman wrote:
    > I have written C++ code for some time now using both pointers and
    > references.
    >
    > I was asked what the point was in using pointers and could not give a
    > short an explanatory example.
    >
    > I would say that you use a pointer when data are updated across function
    > calls.
    >
    > But does anyone have an idea to a minimal example where its impossible
    > to avoid pointer?


    You can have a vector of pointers, but not a vector of references.

    As a particular case of the "reseating" issue already noted by Erik,
    consider a class that may (according to your design philosophy) have a
    member either a reference member or a pointer member. If the target
    object of the pointer or reference (the referee?) is not yet available
    when the referring (pointy?) object is constructed, then a reference is
    out of the question. Code like the following even flags a compile-time
    error from g++:

    struct S {
    S() { }
    void set_i(int*);
    private:
    int& i_;
    };

    g++ -ansi -pedantic -Wall -c -o main.o main.cc
    main.cc: In constructor 'S::S()':
    main.cc:2: error: uninitialized reference member 'S::i_'
    make: *** [main.o] Error 1

    A pointer, by contrast, can be temporarily initialized to a reasonable
    default value, and later assigned a more permanent value.

    A pointer is an object in its own right, with its very own address in
    memory. A reference is just a way of referring to an existing object,
    and may or may not require run-time support from pointer-like entities.
    You can have a reference to a pointer, but not a reference to a reference.
    Jeff Schwab, Feb 14, 2008
    #7
  8. saneman

    Salt_Peter Guest

    On Feb 13, 5:11 pm, saneman <> wrote:
    > wrote:
    > > [8.6] When should I use references, and when should I use pointers?
    > >http://www.parashift.com/c -faq-lite/references.html#faq-8.6

    >
    > Thanks but I was looking for some code sample that illustrates the
    > differences. In the below code 'test' takes a pointer to a list. But I
    > never use 'new' to make it work. Why would it ever be necessary to use
    > 'new' when I can just pass the address to the list to 'test' using '&'?


    What makes you think that pointers and new are related in any way?
    To get back on topic, isn't the code below an example where a
    reference is preferred over a pointer?

    >
    > #include<iostream>
    > #include<list>
    > void test(std::list<int>* l) {
    > l->push_back(555);
    >
    > }


    Note the difference between:

    std::list<int>* l; // pointer can be reseated
    const std::list<int>* c_l; // same, any const list will do
    std::list<int>* const c_l; // const pointer, can't be reseated, list
    is mutable
    const std::list<int>* const c_l_c;

    Thats is, the above function should really have the following
    signature if you absolutely must use a pointer:

    void test(std::list<int>* const l) { ... } // pointer can't be
    reseated

    Which then makes the following better. Its intentions are clear to the
    user:

    void test(std::list<int>& l) { ... }

    >
    > int main() {
    >
    > std::list<int> l;
    > std::list<int>::iterator list_it;
    > l.push_back(1);
    > l.push_back(2);
    > l.push_back(3);
    >
    > for (list_it = l.begin(); list_it != l.end(); list_it++) {
    > printf("%d\n",*list_it);
    > }
    > std::cout<<std::endl;
    > test(&l);
    > for (list_it = l.begin(); list_it != l.end(); list_it++) {
    > printf("%d\n",*list_it);
    > }
    > return 0;
    >
    > }
    Salt_Peter, Feb 14, 2008
    #8
  9. saneman

    Salt_Peter Guest

    On Feb 13, 5:24 pm, Szabolcs Ferenczi <>
    wrote:
    > On Feb 13, 10:57 pm, saneman <> wrote:
    >
    > > But does anyone have an idea to a minimal example where its impossible
    > > to avoid pointer?

    >
    > Think about polymorphism in C++.
    >
    > Best Regards,
    > Szabolcs


    Polymorphism works quite nicely with references too.
    Salt_Peter, Feb 14, 2008
    #9
  10. saneman

    saneman Guest

    Daniel T. wrote:
    > saneman <> wrote:
    >> wrote:

    >
    >>> [8.6] When should I use references, and when should I use pointers?
    >>> http://www.parashift.com/c -faq-lite/references.html#faq-8.6

    >> Thanks but I was looking for some code sample that illustrates the
    >> differences. In the below code 'test' takes a pointer to a list. But I
    >> never use 'new' to make it work. Why would it ever be necessary to use
    >> 'new' when I can just pass the address to the list to 'test' using '&'?

    >
    > That's a change of subject. First you ask "why pointers?", now you are
    > asking "why new?".
    >
    > What is it that you really want to ask?


    Sorry for the confusion. I would like an example where its impossible to
    avoid use 'new' (dynamic allocation).

    The reason is that I often use pointers and references but seldom the
    keyword 'new'.
    saneman, Feb 14, 2008
    #10
  11. saneman wrote:
    > But does anyone have an idea to a minimal example where its impossible
    > to avoid pointer?


    Try creating a linked list without pointers.
    Juha Nieminen, Feb 14, 2008
    #11
  12. saneman

    Lionel B Guest

    On Thu, 14 Feb 2008 14:00:10 +0200, Juha Nieminen wrote:

    > saneman wrote:
    >> But does anyone have an idea to a minimal example where its impossible
    >> to avoid pointer?

    >
    > Try creating a linked list without pointers.


    Sure it's possible... clunky, painful and probably inefficient, but
    possible (e.g. use arrays for storage and arrays of indices for access).

    --
    Lionel B
    Lionel B, Feb 14, 2008
    #12
  13. saneman

    Pete Becker Guest

    On 2008-02-14 10:35:15 -0500, Lionel B <> said:

    > On Thu, 14 Feb 2008 14:00:10 +0200, Juha Nieminen wrote:
    >
    >> saneman wrote:
    >>> But does anyone have an idea to a minimal example where its impossible
    >>> to avoid pointer?

    >>
    >> Try creating a linked list without pointers.

    >
    > Sure it's possible... clunky, painful and probably inefficient, but
    > possible (e.g. use arrays for storage and arrays of indices for access).


    But given that it's hypothetically impossible to give such an example
    (a Turing machine doesn't have pointers), examples that are clunky,
    painful, and inefficient without pointers are good answers.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
    Pete Becker, Feb 14, 2008
    #13
  14. saneman

    Daniel T. Guest

    saneman <> wrote:

    > Sorry for the confusion. I would like an example where its impossible to
    > avoid use 'new' (dynamic allocation).
    >
    > The reason is that I often use pointers and references but seldom the
    > keyword 'new'.


    You seldom use the keyword 'new'? Sounds like you have answered your own
    question. Try to go back and do the same thing without the keyword. :)

    In C++, you should be using 'new' very rarely.
    Daniel T., Feb 15, 2008
    #14
  15. saneman

    arnuld Guest

    > On Fri, 15 Feb 2008 07:19:02 -0500, Daniel T. wrote:

    > In C++, you should be using 'new' very rarely.


    Oh.. I did not know that :(



    --
    http://lispmachine.wordpress.com/
    arnuld, Feb 27, 2008
    #15
    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. Phil
    Replies:
    1
    Views:
    639
    llewelly
    Sep 16, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,763
    Smokey Grindel
    Dec 2, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
  5. ArifulHossain tuhin

    Why pointers to pointers used

    ArifulHossain tuhin, Feb 28, 2012, in forum: C Programming
    Replies:
    16
    Views:
    451
    Stephen Sprunk
    Mar 1, 2012
Loading...

Share This Page