C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 27 Jan 2025 10:34:12 +0000
I reply to Bo and Tiago in series below:


Bo wrote:
>
> Or (3) All pointers to class type are the same size.
>
> Pointers to non-class types, like the earlier example of char* can still
> have a different size.


So you envisage something like the following on a 32-Bit system whose
CPU instructions deal with operands with a minimum size of 32 bits?

    sizeof(int*) == 4
    sizeof(void*) == sizeof(char*) == sizeof(short*) == sizeof( any
class type pointer ) == 5



Tiago wrote:
>
> So what is the point of this?


I want C++26 to explicitly state: "All pointer types are
intercompatible -- they all have the same size and representation",
which would allow code like:

    char c;
    long double *p = &c;
    char *p2 = p;
    *p2 = 'a';

The main use for this would be in allowing data to be misaligned in
memory. For example let's consider "std::basic_string<char32_t>" which
contains a pointer into its own buffer. If such an object is
misaligned in memory, then the pointer into the buffer might not
accurately point at the buffer. But I want the Standard to be changed
so that all pointers are the same.

There were rumours years and years ago of systems that had different
size pointers but they're all extinct now.

Received on 2025-01-27 10:34:19