C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 28 Jan 2025 15:29:05 +0000
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.

Received on 2025-01-28 15:29:18