Re: Pointers: What's the point?

Discussion in 'C++' started by foo, Jun 24, 2003.

  1. foo

    foo Guest

    (Michiel Salters) wrote in message news:<>...
    > "no_sp@m" <no_sp@m> wrote in message news:<3ef5f7a9$>...
    > > Pointers and references behave exactly the same, except the syntax of
    > > how you write them and use them is different.

    >
    > Wrong. There are a number of differences:
    >
    > * pointers can be NULL, or not initialized
    > * temporaries can be bound to const references only.
    > * Pointers are objects and can be memcpy'ed
    > * Copy ctors must use references
    > * Pointer values can be changed, references stay bound to
    > the same object during their lifetime.
    > * Pointers support pointer arithmetic.
    >
    > That's not quite "exactly the same".
    >
    > > That said, we can rephrase
    > > the question, "What's the point of pointers and references?" to just
    > > "What's the point of references?" (or, "What's the point of pointers?").

    >
    > You can't. Some things require pointers, some things require references.
    >
    > > Answer:
    > > 1. Pointers and references allow objects to be stored and passed as
    > > parameters by copying just the *address* of the object ("pass by
    > > reference"), not the entire object ("pass by value"). They also allow
    > > objects to be returned by reference (careful, though: the object must
    > > exist outside the method, e.g., as a global variable or as "this").
    > > 2. Pointers and references force "automatic updating" (my term). i.e.,
    > > If two copies of a pointer exist and the value of one is changed, then
    > > the value of the other changes, too. This is useful for changing
    > > parameters inside methods, and sharing values between instances of classes.

    >
    > Untrue. If you change the value of a pointer, it points to a different
    > object. You're confusing the value of a pointer and the value it points
    > to.
    > int a =1; int b=2;
    > int ptr = &a;
    > // Change the value of ptr
    > ptr = &b;
    > // Change the value ptr points to
    > ++*ptr; // b is now 3
    >
    > Because references aren't objects they don't *have* a value. They
    > only refer/point to values.
    >
    > > 3. Pointers allow you to declare a space to hold arrays when the size is
    > > not known until run time (i.e., you must use the "new" operator).

    >
    > Precisely, new and delete don't work on references.


    Wrong. :)
    You can use new and delete on references.
    Example:
    int &x = *(new int());
    delete &x;

    While I agree that you shouldn't use new and delete on references,
    that doesn't mean that you can't.
     
    foo, Jun 24, 2003
    #1
    1. Advertising

  2. Some comments:


    >>Wrong. There are a number of differences:
    >>
    >>* pointers can be NULL, or not initialized

    What's the point of not initializing a pointer? How do you know your
    pointer is pointing to a real object or into nirvana?

    >>* temporaries can be bound to const references only.
    >>* Pointers are objects and can be memcpy'ed
    >>* Copy ctors must use references

    Who said that ctors MUST use references? You can equally well use a
    pointer for that... yeah, you should be a litte bit more careful, to not
    confuse people, but that't another story :)

    >>* Pointer values can be changed, references stay bound to
    >> the same object during their lifetime.

    Well this makes the assumption, that you are are talking about non-const
    pointers

    >>* Pointers support pointer arithmetic.
    >>
    >>That's not quite "exactly the same".
    >>
    >>
    >>>That said, we can rephrase
    >>>the question, "What's the point of pointers and references?" to just
    >>>"What's the point of references?" (or, "What's the point of pointers?").

    >>
    >>You can't. Some things require pointers, some things require references.

    What is it that would require a reference? I would say it more the
    following way: You can do everything with pointers, but some things are
    easier and more clear with references.
     
    Matthias Reik, Jun 26, 2003
    #2
    1. Advertising

  3. "Matthias Reik" <> wrote...
    > Victor Bazarov wrote:
    > > "Matthias Reik" <> wrote...
    > >
    > >>Some comments:
    > >>
    > >>
    > >>
    > >>>>Wrong. There are a number of differences:
    > >>>>
    > >>>>* pointers can be NULL, or not initialized
    > >>>
    > >>What's the point of not initializing a pointer?

    > >
    > >
    > > The point is to save time. If you know that the pointer is
    > > going to be calculated somehow, why initialise it to anything
    > > before that?

    > Saving time is a good idea, but often you are just building your own
    > traps. It often works fine as long as you are following the standard
    > execution path, but (very often) as soon as something unexpected
    > happens, your assumtpions will be wrong. Yes, in some cases you
    > don't need to do this, but would you consider this being the rule?
    > I wouldn't


    I didn't say it was THE rule.

    > >>How do you know your
    > >>pointer is pointing to a real object or into nirvana?

    > >
    > >
    > > You know if you made it point somewhere.

    > So you need to keep track whether it's pointing somewhere? Wouldn't
    > that be easier if you would init it to NULL


    No, it wouldn't. See above. If it's I who is writing the code,
    *I* know whether I initialised it or not. If it's somebody else's
    pointer coming into my function, you bet your ass I will check it
    for being null.

    > >>>>* temporaries can be bound to const references only.
    > >>>>* Pointers are objects and can be memcpy'ed
    > >>>>* Copy ctors must use references
    > >>>
    > >>Who said that ctors MUST use references?

    > >
    > >
    > > The Standard said that.

    > Are you REALLY sure, I don't think so (even so I haven't got the
    > standard in reachable distance).


    Well, ignorance is a bliss, ain't it? Once you get a copy of the
    standard in reachable distance, reach for it and read 12.1/10.

    > >>You can equally well use a
    > >>pointer for that... yeah, you should be a litte bit more careful, to not
    > >>confuse people, but that't another story :)

    > >
    > >
    > > That wouldn't be a copy constructor. It would be just
    > > another parametrised one.
    > >
    > >>>>* Pointer values can be changed, references stay bound to
    > >>>> the same object during their lifetime.
    > >>>
    > >>Well this makes the assumption, that you are are talking about non-const
    > >>pointers

    > >
    > >
    > > Feeling like nit-picking today, aren't we?

    > no, but talking about something that is seldomly used. What are you
    > allowed to changed if you have got:
    > fn(Object const* const)?


    I am afraid I don't understand the question, even in the context.
    There are no non-const references (references to non-const objects
    don't count), but there are non-const pointers. _If_ a pointer
    is non-const, you can change it. I imagine that had C++ not had
    non-const pointers, there would be no argument. Const variables
    have to be initalised, and they cannot change. In that situation
    pointers to single objects would behave like references. However,
    (a) we are not in that situation, the language allows to have non-
    const pointers, and (b) pointers are also used to iterate over
    arrays (you really can't do that with references, I'd like to see
    you try, though).

    Victor
     
    Victor Bazarov, Jun 27, 2003
    #3
  4. Victor Bazarov wrote:
    > "Matthias Reik" <> wrote...
    >
    >>Some comments:
    >>
    >>
    >>
    >>>>Wrong. There are a number of differences:
    >>>>
    >>>>* pointers can be NULL, or not initialized
    >>>

    >>What's the point of not initializing a pointer?

    >
    >
    > The point is to save time. If you know that the pointer is
    > going to be calculated somehow, why initialise it to anything
    > before that?

    Saving time is a good idea, but often you are just building your own
    traps. It often works fine as long as you are following the standard
    execution path, but (very often) as soon as something unexpected
    happens, your assumtpions will be wrong. Yes, in some cases you
    don't need to do this, but would you consider this being the rule?
    I wouldn't

    >
    >>How do you know your
    >>pointer is pointing to a real object or into nirvana?

    >
    >
    > You know if you made it point somewhere.

    So you need to keep track whether it's pointing somewhere? Wouldn't
    that be easier if you would init it to NULL

    >
    >
    >>>>* temporaries can be bound to const references only.
    >>>>* Pointers are objects and can be memcpy'ed
    >>>>* Copy ctors must use references
    >>>

    >>Who said that ctors MUST use references?

    >
    >
    > The Standard said that.

    Are you REALLY sure, I don't think so (even so I haven't got the
    standard in reachable distance).

    >
    >>You can equally well use a
    >>pointer for that... yeah, you should be a litte bit more careful, to not
    >>confuse people, but that't another story :)

    >
    >
    > That wouldn't be a copy constructor. It would be just
    > another parametrised one.
    >
    >>>>* Pointer values can be changed, references stay bound to
    >>>> the same object during their lifetime.
    >>>

    >>Well this makes the assumption, that you are are talking about non-const
    >>pointers

    >
    >
    > Feeling like nit-picking today, aren't we?

    no, but talking about something that is seldomly used. What are you
    allowed to changed if you have got:
    fn(Object const* const)?

    >
    >
    >>>>* Pointers support pointer arithmetic.
    >>>>
    >>>>That's not quite "exactly the same".
    >>>>
    >>>>
    >>>>
    >>>>>That said, we can rephrase
    >>>>>the question, "What's the point of pointers and references?" to just
    >>>>>"What's the point of references?" (or, "What's the point of
    >>>>

    > pointers?").
    >
    >>>>You can't. Some things require pointers, some things require references.
    >>>

    >>What is it that would require a reference? I would say it more the
    >>following way: You can do everything with pointers, but some things are
    >>easier and more clear with references.

    >
    >
    > One thing you can do with references that you cannot do with
    > pointers: rest assured that in a legal program there is always
    > an object behind a reference. Once you have a reference, you
    > have an object. Not so with pointers.
    >
    > Victor
    >
    >
     
    Matthias Reik, Jun 27, 2003
    #4
  5. "Matthias Reik" <> schrieb im Newsbeitrag
    news:...
    ....
    > >>You can't. Some things require pointers, some things require references.

    > What is it that would require a reference? I would say it more the
    > following way: You can do everything with pointers, but some things are
    > easier and more clear with references.
    >

    challenge:
    given the following codesnippet:

    #include <iostream>

    struct A {
    A() : a(42) {}
    int a;
    };

    // some operator<< ()

    int main() {
    A a;

    std::cout << "Hello world. And the answer is: " << a << std::endl;
    }

    Could you declare and an operator<<(), _without using references_, so that:
    1. the code compiles
    2. it creates reasonable output.

    Much fun

    Norbert
     
    Norbert Riedlin, Jul 1, 2003
    #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. Gopi

    point to point protocol

    Gopi, Jul 13, 2004, in forum: VHDL
    Replies:
    1
    Views:
    477
    Mike Treseler
    Jul 13, 2004
  2. H aka N
    Replies:
    15
    Views:
    15,672
    Ben Jones
    Mar 2, 2006
  3. Motaz Saad
    Replies:
    7
    Views:
    6,495
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    683
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,350
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page