C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Make all data pointers intercompatible

From: Jeremy Rifkin <rifkin.jer_at_[hidden]>
Date: Tue, 28 Jan 2025 10:41:59 -0600
> I want the C++26 Standard to say that the implementation shall have
pointers into data memory, and that the size of these pointers shall
be implementation-defined (e.g. 32-Bit or 64-Bit).
All data pointer types are exactly the same (same size, same
alignment, same representation).
Data pointer arithmetic is well-defined for all data pointer types,
even if the pointer contains a value which is misaligned. So if we
have a pointer to a misaligned double:

On one hand I understand where you're coming from and why this might be
desirable. But, on the other hand, it has always been perceived by WG14 and
WG21 that there's value in not over-constraining. CHAR_BIT is a great
example of this, though that may be constrained by P3477 due to
practicality. Pointer constraints might fall into this bucket too.

But what's the motivation? What do you need to write that you can't now but
could if pointers had different guarantees?

Cheers,
Jeremy

On Tue, Jan 28, 2025 at 9:29 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

> On Tue, Jan 28, 2025 at 3:16 PM organicoman wrote:
> >
> > The resulting pointer is unspecified: means dereferencing it is UB.
> > Because the compiler is not allowed to alter the bits of A. It will keep
> it as given.
> >
> > I can do the following.
> >
> > double* ptr = reinterpret_cast<double*>(0x7fff934a9d83); // misaligned
> for double
> >
> > ptr value will stay 0x7fff934a9d83, but it is unusable otherwise:
> > *ptr is UB
> > ++ptr is UB
> > --ptr is UB
> > ptr+int the result is UB.
> > ...etc
>
>
> I want the C++26 Standard to say that the implementation shall have
> pointers into data memory, and that the size of these pointers shall
> be implementation-defined (e.g. 32-Bit or 64-Bit).
> All data pointer types are exactly the same (same size, same
> alignment, same representation).
> Data pointer arithmetic is well-defined for all data pointer types,
> even if the pointer contains a value which is misaligned. So if we
> have a pointer to a misaligned double:
>
> double *ptr = reinterpret_cast<double*>(0x7fff934a9d83);
>
> Then I want "++ptr" to add 8 to it, giving 0x7FFF934A9D8B. I want this
> to be well-defined behaviour.
>
> Of course though, to dereference it would still be UB.
>
> By the way . . . . . just in case anyone's wondering . . . . . what
> I'm asking for is already possible with all modern C++ compilers, I
> just want it to be written in stone in the Standard.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-01-28 16:42:14