Pointers consistency

D

Dominic Genest

Hi,

If two pointers point to the same memory block, can I be absolutely
sure, according to official standard C++ specifications, that the
expression, at any time in the program, even if the operating system
decided to cache or move memory blocks or anything, "p1==p2" will
evaluate to true ?

Of course, if "p1==p2" is true, I know that I can conclude that p1 and
p2 point to the same object. But what about the inverse ? If p1 and p2
point to the same object, will p1 and p2 be the same for sure ? Even
after casts ?

Could someone point to some official documentation that would tell me
so for C++ standards ?

Thanks a lot !

Dom
 
V

Victor Bazarov

Dominic said:
If two pointers point to the same memory block,

"memory block" is not a standard C++ concept, AFAICT. Do you mean
"object"?
can I be absolutely
sure, according to official standard C++ specifications, that the
expression, at any time in the program, even if the operating system
decided to cache or move memory blocks or anything, "p1==p2" will
evaluate to true ?

The Standard says "Two pointers of the same type
compare equal if and only if they are both null, both point to
the same object or function, or both point one
past the end of the same array."

As you can see, there is nothing about OS deciding to cache anything,
nothing about moving blocks.
Of course, if "p1==p2" is true, I know that I can conclude that p1 and
p2 point to the same object. But what about the inverse ? If p1 and p2
point to the same object, will p1 and p2 be the same for sure ? Even
after casts ?

If p1 and p2 are of two different types, they will have to be converted
to the same type before comparison. What other 'casts' are you talking
about?

Expressions /point to the same object/ and /equal/ WRT to two pointers
mean the same thing. IOW, if one is true, the other is true as well.
Could someone point to some official documentation that would tell me
so for C++ standards ?

I do not understand this question. There is the official C++ standard,
what other C++ standards are you looking for?

V
 
A

Alan Johnson

Dominic said:
Hi,

If two pointers point to the same memory block, can I be absolutely
sure, according to official standard C++ specifications, that the
expression, at any time in the program, even if the operating system
decided to cache or move memory blocks or anything, "p1==p2" will
evaluate to true ?

Of course, if "p1==p2" is true, I know that I can conclude that p1 and
p2 point to the same object. But what about the inverse ? If p1 and p2
point to the same object, will p1 and p2 be the same for sure ? Even
after casts ?

Could someone point to some official documentation that would tell me
so for C++ standards ?

Thanks a lot !

Dom

No. In particular if you are using multiple inheritance and
reinterpret_cast, many implmentations will return a different pointer
value. As far as what the standard has to say about it:

Section 5.2.10, Line 3 :
"The mapping performed by reinterpret_cast is implementation-defined.
[Note: it might, or might not, produce a representation different from
the original value. ]"

Alan
 
B

Buster

Dominic said:
If two pointers point to the same memory block, can I be absolutely
sure, according to official standard C++ specifications, that the
expression, at any time in the program, even if the operating system
decided to cache or move memory blocks or anything, "p1==p2" will
evaluate to true ?

Of course, if "p1==p2" is true, I know that I can conclude that p1 and
p2 point to the same object.

I wanted to write that that is not guaranteed (I was thinking about a
32-bit computer with more than 4GiB of memory) but apparently (5.10/1)
it is.
But what about the inverse ? If p1 and p2
point to the same object, will p1 and p2 be the same for sure?

Yes (ibid.).
Even after casts?

Yes: if the values of two cast-expressions are pointers to one and the
same object, then (it follows from the foregoing) the result of
comparing those values for equality is the value true. Do think about
multiple inheritance, but I don't think it's relevant (and I think this
is what Victor said): p1 and p2 were the same type to begin with, and if
you cast both of them to some one other type, the same transformation is
applied to p1 as to p2, so the relation between p1 and p2 is the same as
the relation between the values of the respective casts.
Could someone point to some official documentation that would tell me
so for C++ standards ?

You want 5.10 [expr.eq].
The wording is unequivocal ("... if and only if ...").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top