Yes, that is the definition of raw pointer equality: they both point to the same location (or are pointer aliases); usually in the virtual address space of the process running your application coded in C++ and managed by some operating system (but C++ can also be used for programming embedded devices with micro-controllers having a Harward architecture: on such microcontrollers some pointer casts are forbidden and makes no sense - since read only data could sit in code ROM)
The concepts and terminology of garbage collection are also relevant when managing pointers and memory zones obtained by dynamic allocation (e.g. ::operator new), so read perhaps the GC handbook.
For a bit of facts here is the relevant text from the specifications
Equality operator (==,!=)
Pointers to objects of the same type can be compared for equality with the 'intuitive' expected results:
From § 5.10 of the C++11 standard:
Pointers of the same type
(after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if
and only if they are both null, both point to the same function, or both represent the same address (3.9.2).
(leaving out details on comparison of pointers to member and or the null pointer constants - they continue down the same line of 'Do What I Mean':)
[...] If both operands are null, they compare equal. Otherwise if only one is null, they compare unequal.[...]
The most 'conspicuous' caveat has to do with virtuals, and it does seem to be the logical thing to expect too:
[...] if either is a pointer to a virtual member function, the result is unspecified. Otherwise they
compare equal if and only if they would refer to the same member of the same most derived object (1.8)
or the same subobject if they were dereferenced with a hypothetical object of the associated class type. [...]
Relational operators (<,>,<=,>=)
From § 5.9 of the C++11 standard:
Pointers to objects or functions of the same type (after pointer conversions) can be compared,
with a result defined as follows:
If two pointers p and q of the same type point to the same object or
function, or both point one past the end of the same array, or are both
null, then p<=q and p>=q both yield true and p<q and p>q both yield false.
If two pointers p and q of the same type point to different objects that are
not members of the same object or elements of the same array or to different
functions, or if only one of them is null, the results of p<q,p>q,p<=q, and
p>=qare unspecified.
If two pointers point to non-static data members of the same object, or to
subobjects or array elements of such members, recursively, the pointer to the
later declared member compares greater provided the two members have the same
access control (Clause 11) and provided their class is not a union.
If two pointers point to non-static data members of the same object with
different access control (Clause 11) the result is unspecified.
If two pointers point to non-static data members of the same union object,
they compare equal (after conversion to void*, if necessary). If two pointers
point to elements of the same array or one beyond the end of the array, the
pointer to the object with the higher subscript compares higher.
Other pointer comparisons are unspecified.
So, if you had:
int arr[3];
int *a = arr;
int *b = a + 1;
assert(a != b); // OK! well defined
Also OK:
struct X { int x,y; } s;
int *a = &s.x;
int *b = &s.y;
assert(b > a); // OK! well defined
But it depends on the something in your question:
int g;
int main()
{
int h;
int i;
int *a = &g;
int *b = &h; // can't compare a <=> b
int *c = &i; // can't compare b <=> c, or a <=> c etc.
// but a==b, b!=c, a!=c etc. are supported just fine
}
Bonus: what else is there in the standard library?
§ 20.8.5/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."
So, you can globally order any odd void* as long as you use std::less<> and friends, not bare operator<.
To sum up. If we want to see if two pointers point to the same memory location we can do that. Also if we want to compare the contents of the memory pointed to by two pointers we can do that too, just remeber to dereference them first.
int main () {
int a = 10, b = 20;
int *p1, *p2, *p3, *p4;
p1 = &a;
p2 = &a;
if(p1 == p2){
std::cout<<"p1 and p2 alias each other"<<std::endl;
}
else{
std::cout<<"p1 and p2 do not alias each other"<<std::endl;
}
//------------------------
p3 = &a;
p4 = &b;
if(p3 == p4){
std::cout<<"p3 and p4 alias each other"<<std::endl;
}
else{
std::cout<<"p3 and p4 do not alias each other"<<std::endl;
}
return 0;
}
Output:
p1 and p2 alias each other
p3 and p4 do not alias each other
It depends on the types of the values, and the way that operators happen to have been defined. For example, string comparison is by value, not by address. But char * is by address normally (I think).
A big trap for the unwary. There is no guaranteed pointer comparison operator, but