Yastanub
Yastanub

Reputation: 1237

Is (void*) ptr == ptr always true?

I cut this question out of my last question because i thought this was rather an individual question. So i found the passages for pointer conversion in the standard from which the ones regarding my question are:

6.3.2.3

Pointers

1 A pointer to void may be converted to or from a pointer to any object type. A pointer toany object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

...

4 Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.

Now it only states that

(originaltype*)((void*)ptr) == ptr

shall always be true but what about

(void*) ptr == ptr

It is not explicitely stated wether this shall be true or false. Or do i misinterpred the 1 paragraph?

Upvotes: 3

Views: 395

Answers (2)

Kaz
Kaz

Reputation: 58627

*If ptr is a pointer to an object type then (void *) ptr == ptr is equivalent to (void *) ptr == (void *) ptr. The ptr on the right is implicitly converted to void *. (If it's a pointer to a const or volatile qualified type, those qualifiers are lost in the implicit conversion.)

(void *) ptr is certainly equal to itself, unless we entertain idle humor like ptr being a macro expanding to an expression with side effects that change its value on different evaluations, or being an indeterminately-valued expression, the use of which is undefined-behavior.

If ptr is a pointer to a function, then (void *) ptr == ptr requires a diagnostic; but it's clear the discussion is about object types.

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 223795

C 2018 6.5.9 discusses ==. Paragraph 2 specifies constraints, and (void *) ptr == ptr satisfies the constraints because one of the options is “one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void”. Then paragraph 5 says “… If one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void, the former is converted to the type of the latter.”

Thus, in (void *) ptr == ptr, the right operand is converted to (void *), so the expression is equivalent to (void *) ptr == (void *) ptr, and we may expect it evaluates to true.

Strictly speaking, the clause on pointer conversion, 6.3.2.3, tells us only that the result of converting (void *) ptr back to its original type will compare equal to ptr. It does not tell us anything else about the value of (void *) ptr, and so, considering only this clause, it is possible that two different instances of (void *) ptr will produce different results, as long as they contain sufficient information to produce something that will compare equal to the original ptr when converted back.

Back at 6.5.9, paragraph 6 tells us:

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

Now, certainly we expect (void *) ptr == (void *) ptr to be true at least some of the time. How is this possible? (void *) ptr is not a null pointer (assuming ptr was not), nor do we expect this case is covered by the end of one array and start of another case. So we expect that, when (void *) ptr == (void *) ptr evaluates to true, it must be because it is in the “pointers to the same object” case or the “pointers to one past the last element of the same array object case”. That seems like the only reasonable way to interpret the standard. If so, then this case (whichever one is the one that applies sometimes) must apply all the time, and the “if and only if” tells us that (void *) ptr == (void *) ptr is always true.

Upvotes: 4

Related Questions