Hacker News new | ask | show | jobs
by uecker 7 days ago
I hear this claim sometimes, but I think this is a far-fetched interpretation that you can only compare the pointers after backconversion but not use them.

A segmented architecture that can not support this should not support intptr_t. It is optional for this reason.

1 comments

The C standard does not make any guarantees that you can dereference it.

And x86-64 is arguably still "segmented" in that you can use the FS and GC segment registers (they're typically used for TLS). Eg, if you cast a `thread_local` pointer to `intptr_t` - the standard makes no requirement that it point to the absolute virtual address (though this is the common implementation choice by GCC etc).

The standard is English text which may not alway be perfectly precise, but the wording there clearly was meant to allow this.
I think it's quite clear.

> An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, may not be correctly aligned, may not point to an entity of the referenced type, and can produce an indeterminate representation when stored into an object.

That isn't to say we can't do the cast - compilers specify the behavior, and most modern architectures have a linear virtual address space where the round trip is valid - though it may not always be - for example, with the fs/gs segment registers - a cast of pointer to integer isn't just a simple conversion of type - it must also fetch the value from the FS register and offset the pointer by this to produce an integer containing the absolute address. This is entirely implementation defined.

Producing the absolute virtual address of a `thread_local` is potential UB though - if you attempt to dereference it in a different thread.

Another example would be casting a qualified pointer (const, restrict, volatile) to an integer, then back to a pointer with a different qualifier or no qualifier - then dereferencing such pointer would of course be UB as per the standard - the cast to `intptr_t` loses information about its qualification - but the pointers would still compare equal - as the standard guarantees that a pointer cast to a qualified pointer compares equal.

There are stricter requirements for casting a void pointer to (u)intptr_t and back. Again, a segmented architecture should not provide (u)intptr_t. But if an implementation has it, in my opinion, this is expected to work.
The only stricter requirement in the standard is that they compare equal after the round trip.

The intended purpose of these types is to allow the round trip, yes, and the requirement they compare equal is part of that. The standard doesn't put stricter requirements. I don't think that is an oversight, but a deliberate omission to allow for flexibility in implementation.

It's up to the compiler to specify that it's safe to dereference the pointer after the round trip - the standard does not want to put such constraints on implementors.

This is where I disagree. In the context when this was written, I think it was clear that two pointers that compare equally were meant to be interchangeable.
FTR C++ makes the guarantee. I'm not familiar enough with C standard wording to comment on that.