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




Sent from my Galaxy


-------- Original message --------
From: Sebastian Wittmeier via Std-Proposals <std-proposals@lists.isocpp.org>
Date: 1/28/25 3:40 PM (GMT+01:00)
To: std-proposals@lists.isocpp.org
Cc: Sebastian Wittmeier <wittmeier@projectalpha.org>
Subject: Re: [std-proposals] Make all data pointers intercompatible

According to CPP Reference this is currently not okay:

"If expression represents the address A of a byte in memory but A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified."

https://en.cppreference.com/w/cpp/language/static_cast
 

-----Ursprüngliche Nachricht-----
Von: organicoman via Std-Proposals <std-proposals@lists.isocpp.org>
No it could not, and it is not allowed. The only thing you will MAYBE get is a misaligned pointer to a double memory location, and you should not dereference that pointer, because of aliasing incompatibility otherwise UB.
 
Try the following code:
#include <stdio.h>

int main()
{
    char arr[8];
    double* ptr = static_cast<double*>(static_cast<void*>(arr+3));