Pointer arithmetic

Discussion in 'C++' started by flopbucket, Jun 30, 2006.

  1. flopbucket

    flopbucket Guest

    Hi,

    Quick question regarding pointer math. I have a simple container class
    - something like std::vector<> - that I am implementing.

    My iterators are implemented as pointers:

    template<class T> class myContainer
    {
    typedef T * iterator;
    typedef const T* const_iterator;

    ......
    };

    My question is, for implementing something like distance(), I have
    somthing like:

    unsigned long distance(myContainer<T>::iterator& a,
    myContainer<T>::iterator& b)
    {
    return static_cast<unsigned long>(b-a);
    }

    This works, and I think it must always work, but I wanted to check here
    with some of the experts.

    Also, do you see a better way of doing this?

    Thanks
     
    flopbucket, Jun 30, 2006
    #1
    1. Advertising

  2. flopbucket

    Bo Persson Guest

    "flopbucket" <> skrev i meddelandet
    news:...
    > Hi,
    >
    > Quick question regarding pointer math. I have a simple container
    > class
    > - something like std::vector<> - that I am implementing.
    >
    > My iterators are implemented as pointers:
    >
    > template<class T> class myContainer
    > {
    > typedef T * iterator;
    > typedef const T* const_iterator;
    >
    > .....
    > };
    >
    > My question is, for implementing something like distance(), I have
    > somthing like:
    >
    > unsigned long distance(myContainer<T>::iterator& a,
    > myContainer<T>::iterator& b)
    > {
    > return static_cast<unsigned long>(b-a);
    > }
    >
    > This works, and I think it must always work, but I wanted to check
    > here
    > with some of the experts.


    It seems pretty ok, except that you don't (portably) know the size of
    unsigned long. Also, if you want to compute distance(b, a) you need a
    signed type. Perhaps std::ptrdiff_t ?

    >
    > Also, do you see a better way of doing this?


    The std::distance is already there, and works well for pointers.


    Bo Persson
     
    Bo Persson, Jun 30, 2006
    #2
    1. Advertising

  3. flopbucket

    flopbucket Guest

    Bo Persson wrote:
    > >
    > > Also, do you see a better way of doing this?

    >
    > The std::distance is already there, and works well for pointers.
    >
    >


    Yes, that's true. The thing here is that we are developing for an
    embedded platform. We have a C++ compiler but the vendor supplies no
    C++ libraries, and a highly stripped version of the standard C lib. So
    we are trying to implement just a few basic containers and such.

    Thanks for the reply.
     
    flopbucket, Jun 30, 2006
    #3
  4. flopbucket

    Guest

    flopbucket wrote:

    > The thing here is that we are developing for an
    > embedded platform. We have a C++ compiler but the vendor supplies no
    > C++ libraries, and a highly stripped version of the standard C lib.


    Probably stating the obvious for quite a few people here, but why not
    buy
    such a library? IIRC, Dinkumware has such a lib.

    HTH,
    Michiel Salters
     
    , Jun 30, 2006
    #4
  5. flopbucket

    flopbucket Guest

    wrote:
    > flopbucket wrote:
    >
    > Probably stating the obvious for quite a few people here, but why not
    > buy
    > such a library? IIRC, Dinkumware has such a lib.


    Yes, that certainly was an option. We looked at a few freely available
    ones, such as STLport, but they were too large, needed a more complete
    C library then the platform provides, etc. We discussed possibly
    purchasing one - we found at least one "mini" STL product for embedded
    environments, but in the end, the decision was to roll our own minimal
    implementation for the time being (part of this had to do with special
    memory constraints, non-standard platform API for allocation, etc.,
    although I suppose we could have made a custom allocator to solve most
    of this).

    The group decided they just wanted a few containers (vector, string,
    list) so that's how I ended up doing this work. For the parts
    implemented, it matches the standard so hopefully in the future the
    custom code can be switched out if our platform vendor ever ships a
    more complete library. And while I was outside the decision loop, I
    must admit I have enjoyed this work as I got a chance to dig into some
    implementation details and get a better understanding of the STL.

    I have not looked too closely at the Dinkumware library, perhaps it
    could have fit our needs. However, we really needed pretty much
    standalone containers. We didn't want to have to pull in lots of other
    code and dependencies or modify the supplied 3rd party code. For
    example, having operator<< defined for std::string would require us to
    remove it since that would depend on iostreams, and there is no way we
    could build that given our very limited C library (nor would we want
    to, there is no display or anything on our device).
     
    flopbucket, Jun 30, 2006
    #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. a

    pointer arithmetic

    a, Sep 11, 2003, in forum: C++
    Replies:
    6
    Views:
    532
  2. Marc Schellens

    Iterator/pointer arithmetic

    Marc Schellens, Dec 5, 2003, in forum: C++
    Replies:
    15
    Views:
    860
    tom_usenet
    Dec 8, 2003
  3. dan

    Pointer arithmetic

    dan, Jan 6, 2004, in forum: C++
    Replies:
    1
    Views:
    370
    Jeff Schwab
    Jan 6, 2004
  4. ceo
    Replies:
    8
    Views:
    366
    Pete Becker
    Mar 10, 2005
  5. joshc
    Replies:
    5
    Views:
    588
    Keith Thompson
    Mar 31, 2005
Loading...

Share This Page