Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-12-20


73. Pointer equality

Section:7.6.10  [expr.eq]    Status:TC1    Submitter:Nathan Myers    Date:13 Nov 1998

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.

I think this may be a defect, in that the quoted paragraph extendsoperator=='s domain too far beyond operator<'s. It should permit(but not require) an off-the-end pointer to compare equal to anotherobject, but not to any element of the same array.

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.

Proposed Resolution (10/00):

(See also paper J16/00-0011 = WG21 N1234.)




[8]ページ先頭

©2009-2026 Movatter.jp