Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer arithmetic

Reply
Thread Tools

Pointer arithmetic

 
 
flopbucket
Guest
Posts: n/a
 
      06-30-2006
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

 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      06-30-2006

"flopbucket" <(E-Mail Removed)> skrev i meddelandet
news:(E-Mail Removed) oups.com...
> 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:trdiff_t ?

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


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


Bo Persson


 
Reply With Quote
 
 
 
 
flopbucket
Guest
Posts: n/a
 
      06-30-2006

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.

 
Reply With Quote
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      06-30-2006

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

 
Reply With Quote
 
flopbucket
Guest
Posts: n/a
 
      06-30-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments