Topic: pointers with operator< and less<>
Author: Rimantas Endriukaitis <rimantas@elsis.com>
Date: 2000/06/15 Raw View
This is a multi-part message in MIME format.
--------------8F8FC3E0327049C1052DF118
Content-Type: text/plain; charset=iso-8859-4
Content-Transfer-Encoding: 7bit
Boris wrote:
>
> A *a1 = new A(); // eg. a1 = 0x01
> A *a2 = new A(); // eg. a2 = 0x02
>
> Now it *could* be that (a1 > a2) is true as this operator is unspecified for
> these pointers??
First of all: is your application multithreaded ? If your answer is
positive than the answer will be positive too. In general it depands on
OS.
--
===================================================
Rimantas Endriukaitis
UAB Elsis
Taikos 106b, Kaunas 3000
tel.: (8-27)311-315(d), 311-316(d), 716-498(n)
fax.: (8-27)310-533
e-mail: rimantas@elsis.com
===================================================
--------------8F8FC3E0327049C1052DF118
Content-Type: text/x-vcard; charset=iso-8859-4;
name="rimantas.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Rimantas Endriukaitis
Content-Disposition: attachment;
filename="rimantas.vcf"
begin:vcard
n:Endriukaitis;Rimantas
tel;fax:(8-27)310-533
tel;home:(8-27)716-498
tel;work:(8-27)311-315, 311-316
x-mozilla-html:FALSE
url:www.elsis.lt
org:UAB Elsis;Konstravimas
adr:;;Taikos106b;Kaunas;;3000;Lithuania
version:2.1
email;internet:rimantas@elsis.com
title:Programuotojas
end:vcard
--------------8F8FC3E0327049C1052DF118--
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Boris" <boris@gtemail.net>
Date: 2000/06/16 Raw View
Rimantas Endriukaitis <rimantas@elsis.com>, news:
39465DC8.6B0D7BBD@elsis.com...
> Boris wrote:
> >
> > A *a1 = new A(); // eg. a1 = 0x01
> > A *a2 = new A(); // eg. a2 = 0x02
> >
> > Now it *could* be that (a1 > a2) is true as this operator is unspecified
for
> > these pointers??
>
> First of all: is your application multithreaded ? If your answer is
> positive than the answer will be positive too. In general it depands on
> OS.
I understand Barry's answer because only parts of the addresses are
compared. But what has operator< and addresses have to do with
multithreading? Or is this just because in general there is no
multithreading support in C++?
Boris
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Michiel Salters <salters@lucent.com>
Date: 2000/06/17 Raw View
Rimantas Endriukaitis wrote:
> Boris wrote:
> > A *a1 = new A(); // eg. a1 = 0x01
> > A *a2 = new A(); // eg. a2 = 0x02
> > Now it *could* be that (a1 > a2) is true as this operator is unspecified for
> > these pointers??
> First of all: is your application multithreaded ? If your answer is
> positive than the answer will be positive too. In general it depands on
> OS.
Nonsense.
I know that's a dangerous word around here, but I'm sticking to it.
True, in general it depends on the OS. An pointer can be a strange beast, with
weird internals, but it might also be a 32-bit integer in disguise. You just
don't know.
What is possible it that the two pointers in the example contain a hidden
base part, which isn't shown (e.g. a "segment" part) but is used in a
comparison.
Since this is not related whatsoever with singlethreading vs. multithreading
the answer is a unconditional "Yes, it *could* be."
If the pointers are flat 32 bits, and they are 0x00000001 and 0x00000002
a1>a2 should be false ("not surprising with knowledge of hardware" clause).
Again, this isn't related to multithreading.
Multithreading has to do with the order of execution of statements, the
program flow and thus with the change of variables. Static relations
or the value of constants are not influenced by multithreading.
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Boris" <boris@gtemail.net>
Date: 2000/06/13 Raw View
I asked this in c.l.c++ two weeks ago but didn't get an answer. I hope
someone in this ng can help.
http://www.boost.org/libs/smart_ptr/shared_ptr.htm:
"[...] the standard mandates that relational operations on pointers are
unspecified (5.9 [expr.rel] paragraph 2) but std::less<> on pointers is
well-defined (20.3.3 [lib.comparisons] paragraph 8)."
1997 C++ Public Review Document, 5.9 [expr.rel] paragraph 2:
"Other pointer comparisons are unspecified."
1997 C++ Public Review Document, 20.3.3 [lib.comparisons] paragraph 8:
"For templates greater, less, greater_equal, and less_equal, the
specializations for any pointer type yield a total order, even if the
built-in operators <, >, <=, >= do not."
I don't have the final C++ standard therefore I hope the paragraphs are the
same.
My question is: Why are relational operations on pointers unspecified but
std::less<> on pointers is well-defined? std::less<> is a call of operator<?
Boris
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Dennett <james@jamesd.demon.co.uk>
Date: 2000/06/13 Raw View
Boris wrote:
> I asked this in c.l.c++ two weeks ago but didn't get an answer. I hope
> someone in this ng can help.
>
> http://www.boost.org/libs/smart_ptr/shared_ptr.htm:
> "[...] the standard mandates that relational operations on pointers are
> unspecified (5.9 [expr.rel] paragraph 2) but std::less<> on pointers is
> well-defined (20.3.3 [lib.comparisons] paragraph 8)."
>
> 1997 C++ Public Review Document, 5.9 [expr.rel] paragraph 2:
> "Other pointer comparisons are unspecified."
>
> 1997 C++ Public Review Document, 20.3.3 [lib.comparisons] paragraph 8:
> "For templates greater, less, greater_equal, and less_equal, the
> specializations for any pointer type yield a total order, even if the
> built-in operators <, >, <=, >= do not."
>
> I don't have the final C++ standard therefore I hope the paragraphs are the
> same.
> My question is: Why are relational operations on pointers unspecified but
> std::less<> on pointers is well-defined? std::less<> is a call of operator<?
>
> Boris
The implementation may define std::less<T *> to be something smarter than
a simple call of operator <.
< is intended to be optimally efficient for comparing objects which are part of
the same array; it does not have to be valid for most comparisons between
arbitrary
pointers.
std::less<> may be less efficient because it has to handle comparisons between
any two pointers to objects of the same type.
-- James Dennett <jdennett@acm.org>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/06/13 Raw View
Boris wrote:
>
> I asked this in c.l.c++ two weeks ago but didn't get an answer. I hope
> someone in this ng can help.
>
> http://www.boost.org/libs/smart_ptr/shared_ptr.htm:
> "[...] the standard mandates that relational operations on pointers are
> unspecified (5.9 [expr.rel] paragraph 2) but std::less<> on pointers is
> well-defined (20.3.3 [lib.comparisons] paragraph 8)."
>
> 1997 C++ Public Review Document, 5.9 [expr.rel] paragraph 2:
> "Other pointer comparisons are unspecified."
>
> 1997 C++ Public Review Document, 20.3.3 [lib.comparisons] paragraph 8:
> "For templates greater, less, greater_equal, and less_equal, the
> specializations for any pointer type yield a total order, even if the
> built-in operators <, >, <=, >= do not."
>
> I don't have the final C++ standard therefore I hope the paragraphs are the
> same.
> My question is: Why are relational operations on pointers unspecified but
> std::less<> on pointers is well-defined? std::less<> is a call of operator<?
There are memory allocation schemes where providing a full internally
consistent set of relational operators between arbitrary pointers is
fairly expensive. On such a machine, direct comparison between pointers
uses a quick mechanism that works properly for pointers within the same
object, but might produce inconsistent and meaningless results for
pointers that don't point to different parts of the same object.
However, the algorithms and containers that use std::less<> (either
directly, or as the default value for a more general comparison
operator) often need to have a total ordering operation in order to be
implemented efficiently. They need this guarantee even if the type
they're operating on is a pointer type, even if the objects whose
pointers are being compared are completely unrelated. Therefore, on such
implementations, std::less<T*> will use a less efficient algorithm that
does yield a total order.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Boris" <boris@gtemail.net>
Date: 2000/06/14 Raw View
James Dennett <james@jamesd.demon.co.uk>, news:
394595AC.F65AC6D9@jamesd.demon.co.uk...
> [...]
> < is intended to be optimally efficient for comparing objects which are
part of
>
> the same array; it does not have to be valid for most comparisons between
> arbitrary
> pointers.
Thanks for the answers!
One last question:
A *a1 = new A(); // eg. a1 = 0x01
A *a2 = new A(); // eg. a2 = 0x02
Now it *could* be that (a1 > a2) is true as this operator is unspecified for
these pointers??
Boris
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Barry Margolin <barmar@genuity.net>
Date: 2000/06/14 Raw View
In article <8i5f9h$k4a$1@news.rrz.Uni-Koeln.DE>,
Boris <boris@gtemail.net> wrote:
>One last question:
>
>A *a1 = new A(); // eg. a1 = 0x01
>A *a2 = new A(); // eg. a2 = 0x02
>
>Now it *could* be that (a1 > a2) is true as this operator is unspecified for
>these pointers??
A better example would be:
a1 = 0x00010010
a2 = 0x00020005
on a system where the high-order 4 bytes of the address are the segment,
and the low-order 4 bytes are the offset. Operator< might compare only the
offset portion of the addresses, since all the bytes of an object must be
within the same segment. Less<A*> would compare both the segment and
offset portions.
--
Barry Margolin, barmar@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]