Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Iterator/pointer arithmetic

Reply
Thread Tools

Iterator/pointer arithmetic

 
 
Marc Schellens
Guest
Posts: n/a
 
      12-05-2003
I have a:

vector<A> V;

an

A* a;

which might point to one element of V.

How to check this most quickly?

I am using for now something like

if( V.size() > 0 && a >= &V[0] && a <= &V[ V.size()] ) { it_is_in();}

which works fine for me, but is this standard conform?
And if not: My application will never run on an embedded system.
Does anybody know any existing standard conform compiler on lets
say any unix-workstation, windows system, mac or even VMS
where the above code would produce not the expected?

thanks,
marc

 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      12-05-2003
Marc Schellens wrote:
> I have a:
>
> vector<A> V;
>
> an
>
> A* a;
>
> which might point to one element of V.
>
> How to check this most quickly?
>
> I am using for now something like
>
> if( V.size() > 0 && a >= &V[0] && a <= &V[ V.size()] ) { it_is_in();}
>
> which works fine for me, but is this standard conform?
> And if not: My application will never run on an embedded system.
> Does anybody know any existing standard conform compiler on lets
> say any unix-workstation, windows system, mac or even VMS
> where the above code would produce not the expected?
>
> thanks,
> marc
>


Pointer arithmetic depends on the properties of the pointer.
All pointers can be compared to NULL (== and !=). Other
comparisons are not guaranteed and may have unexpected behavior.
A pointer can be moved via addition or subtraction. Any other
arithmetic operations (e.g. multiplication, division, shifting)
may induce unexpected behavior.

In many embedded systems, pointers are converted to integers
and compared as integers. This assumes a flat and linear
addressing space, though.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      12-05-2003

"Marc Schellens" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a:
>
> vector<A> V;
>
> an
>
> A* a;
>
> which might point to one element of V.
>
> How to check this most quickly?


There is no portable way to determine whether a completely unknown pointer
points at a particular object.


 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      12-05-2003

"Marc Schellens" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
> I have a:
>
> vector<A> V;
>
> an
>
> A* a;
>
> which might point to one element of V.
>
> How to check this most quickly?
>
> I am using for now something like
>
> if( V.size() > 0 && a >= &V[0] && a <= &V[ V.size()] ) { it_is_in();}
>
> which works fine for me, but is this standard conform?
> And if not: My application will never run on an embedded system.
> Does anybody know any existing standard conform compiler on lets
> say any unix-workstation, windows system, mac or even VMS
> where the above code would produce not the expected?
>
> thanks,
> marc
>


To my knowledge, this can not be done in a standards-conforming way. If,
however, You target a known platform (such as Windows), I would not worry to
much about it if it is debug-code (and it looks very much as it is). You
forgot, however to check if a points to a boundary (eg. at V[0], V[1] or...)
and not to some random place (a = (A*)V[0].field). This requires even
dirtier code
If your code is not debug-code, I suggest you reconsider if your design is
ok.

Kind regards
Peter


 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      12-05-2003
> Pointer arithmetic depends on the properties of the pointer.
> All pointers can be compared to NULL (== and !=). Other
> comparisons are not guaranteed and may have unexpected behavior.


Not true. Two pointers that point to objects (or one past objects) can
always be compared for == and !=.

The trouble is that there are some pointers that are indeterminate -- they
are not null, and they do not point to objects. Such pointers cannot be
compared, or, for that matter, copied.


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      12-05-2003
Andrew Koenig wrote:
>
> > Pointer arithmetic depends on the properties of the pointer.
> > All pointers can be compared to NULL (== and !=). Other
> > comparisons are not guaranteed and may have unexpected behavior.

>
> Not true. Two pointers that point to objects (or one past objects) can
> always be compared for == and !=.


Maybe my memory serves me wrong right now. But what about < and >? If I
remember correctly there is also a guarantee if the pointers point into
the same array.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      12-05-2003
> > Not true. Two pointers that point to objects (or one past objects) can
> > always be compared for == and !=.


> Maybe my memory serves me wrong right now. But what about < and >? If I
> remember correctly there is also a guarantee if the pointers point into
> the same array.


Two pointers to elements of the same array can be compared with <, >, <=,
and >= as well as == and !=. But unless you already know that the pointers
point to elements of the same array, you can't count on the results from
order comparisons.


 
Reply With Quote
 
Eugene Alterman
Guest
Posts: n/a
 
      12-06-2003
"Andrew Koenig" <(E-Mail Removed)> wrote in message
news:3m1Ab.395408$(E-Mail Removed)...
> There is no portable way to determine whether a completely unknown pointer
> points at a particular object.


So you are saying that according to the standard a pointer may compare
equal to the address of an object
without actually pointing to any object, aren't you?
Can this actually happen on any existing platform?


 
Reply With Quote
 
Eugene Alterman
Guest
Posts: n/a
 
      12-06-2003

"Marc Schellens" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a:
>
> vector<A> V;
>
> an
>
> A* a;
>
> which might point to one element of V.
>
> How to check this most quickly?
>
> I am using for now something like
>
> if( V.size() > 0 && a >= &V[0] && a <= &V[ V.size()] ) { it_is_in();}


You mean a <= &V[V.size() - 1] , don't you?
Or, better yet,
if(!V.empty() && a >= &V.front() && a <= &V.back())

> which works fine for me, but is this standard conform?
> And if not: My application will never run on an embedded system.
> Does anybody know any existing standard conform compiler on lets
> say any unix-workstation, windows system, mac or even VMS
> where the above code would produce not the expected?


This is what guaranteed under the latest standard revision (and arguably by
all the known implementations):

"The elements of a vector are stored contiguosly, meaning that if v is a
vector <T, Allocator> where T is some type other that bool, then it obeys
the identity
&v[n] == &v[0] + n for all 0 <= n < v.size()."

The gurus, apparently, don't think it is enough. What do you know...

Still, I believe that in practice (and most certainly on systems with flat
memory model) you are fine.


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      12-06-2003
On Fri, 5 Dec 2003 20:28:54 -0500, "Eugene Alterman"
<(E-Mail Removed)> wrote in comp.lang.c++:

> "Andrew Koenig" <(E-Mail Removed)> wrote in message
> news:3m1Ab.395408$(E-Mail Removed)...
> > There is no portable way to determine whether a completely unknown pointer
> > points at a particular object.

>
> So you are saying that according to the standard a pointer may compare
> equal to the address of an object
> without actually pointing to any object, aren't you?
> Can this actually happen on any existing platform?


No, that's not what he said, nor what he meant.

If you have an array of N objects of type T:

T t_array [N];

....and an unknown pointer to a type T object that you suspect might be
a pointer to one of the N elements of t_array:

T *A;

....then the code:

bool found_it = false;

for (int x = 0; x < N; ++x)
{
if (A == (t_array + x))
{
found_it = true;
}
}

....will have defined behavior and set found_it to true if and only if
A actually does point to one of the elements of t_array.

But if A points to a T object that is separate from t_array, each and
every one of the N comparisons in the loop has undefined behavior.

If the A pointer happened to point to a T object allocated with new,
and then deleted, it is now indeterminate, and just accessing the
pointer's value without dereferencing it could produce undefined
behavior. On a platform with virtual memory, the memory block that A
existed in might have been unmapped from the program's memory space,
and merely loading that value into a pointer register might trigger a
hardware trap that shuts down the program.

Two pointers may be compared only if they both point to elements of
the same array, or to one past the end of an array. What happens if
you break this rule can vary with the hardware platform involved, so
the language standard merely leaves it undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
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
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Java Arithmetic - Using MOD (%) Steven Davies Java 3 02-01-2005 10:25 PM
Performing Arithmetic on DataTable Columns George Durzi ASP .Net 2 06-07-2004 05:23 PM
Arithmetic Libraries ALuPin VHDL 1 02-10-2004 04:37 PM
Synthesisable fixed-point arithmetic package Jonathan Bromley VHDL 1 08-15-2003 03:32 PM



Advertisments