Created on1998-11-13.00:00:00 last changed277 months ago
Proposed Resolution (10/00):
A valid value of an object pointer type represents either the addressof a byte in memory (6.8.1 [intro.memory]) or a null pointer(7.3.12 [conv.ptr]). If an object of typeT islocated at an addressA, a pointer of typecvT* whose value is the addressA is said topointto that object, regardless of how the value was obtained.[Note: for instance, the address one past the end of an array(7.6.6 [expr.add]) would be considered to point to anunrelated object of the array's element type that might be located atthat address.]
Two pointers of the same type compare equal if and only if they areboth null, both point to the same function, or both represent the sameaddress (6.9.4 [basic.compound]).
(See also paper J16/00-0011 = WG21 N1234.)
Nathan Myers: In7.6.10 [expr.eq], we have:
Pointers to objects or functions of the same type (after pointerconversions) can be compared for equality. Two pointers of the same typecompare equal if and only if they are both null, both point to the sameobject or function, or both point one past the end of the same array.What does this say, when we have
int i[1]; int j[1];about the expression(i+1 == j) ? It seems to require paddingbetweeni[0] andj[0] so that the comparison will comeout false.
Mike Miller: I think this is reading more into the statementin7.6.10 [expr.eq]paragraph 1 than is actuallythere. What does it mean for a pointer to "point to" an object?I can't find anything that definitively says thati+1 cannot "pointto"j[0] (although it's obviously not required to do so).Ifi+1 is allowed to "point to"j[0], theni+1==jis allowed to be true, and there's no defect. There are places wherealiasing is forbidden, but the N+1th element of an array doesn't appearto be one of them.
To put it another way, "points to" is undefined in the Standard. Theonly definition I can think of that encompasses the possible ways in whicha pointer can get its value (e.g., the implementation-defined conversionof an arbitrary integer value to a pointer) is that it means "having thesame value representation as would be produced by applying the (builtin)& operator to an lvalue expression designating that object".In other words, if the bits are right, it doesn't matter how you producedthe value, as long as you didn't perform any operations that have undefinedresults. The expressioni+1 is not undefined, so if thebits ofi+1 are the same as those of&j[0], theni+1 "points to"j[0] andi+i==j is allowed tobe true.
Tom MacDonald: C9X contains the following words for the "=="operator:
Two pointers compare equal if both are null pointers, bothare pointers to the same object (including a pointer to an object and asubobject at its beginning) or function, both are pointers to onepast the last element of the same array object, or one is a pointer toone past the end of one array object and the other is a pointer to thestart of a different array object that happens to immediately follow thefirst array object in the address space.Matt Austern: I don't think there's anything wrong with saying thatthe result of
int x[1]; int y[1]; std::cout << (y == x + 1) << std::endl;is implementation defined, or even that it's undefined.
Mike Miller: A similar question could be raised about differentobjects that (sequentially) share the same storage. Consider the following:
struct B { virtual void f(); }; struct D1: B { }; struct D2: B { }; void g() { B* bp1 = new D1; B* bp2 = new (bp1) D2; bp1 == bp2; // ??? }Section6.8.4 [basic.life]paragraph 5 doesnot list this kind of comparison among the pointer operations that causeundefined behavior, so presumably the comparison is allowed. However,7.6.10 [expr.eq]paragraph 1 describes pointer comparison in terms of "[pointing] to thesame object," whichbp1 andbp2 clearly do not do. Howshould we describe the result of this comparison?Jason Merrill:When you consider comparing pointers to void, this seems to suggest that notwo objects can have the same address, depending on your interpretation of"point to the same object." This would cripple the empty baseoptimization.
6.9.4 [basic.compound] refers to 'pointersto void or objects or functions'. In that case,7.6.10 [expr.eq] doesnot allow you to compare them; it only allows comparing pointers toobjects and functions.
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2003-04-25 00:00:00 | admin | set | status: dr -> tc1 |
| 2000-11-18 00:00:00 | admin | set | status: ready -> dr |
| 2000-05-21 00:00:00 | admin | set | status: drafting -> ready |
| 1999-09-14 00:00:00 | admin | set | messages: +msg95 |
| 1998-11-13 00:00:00 | admin | create | |