Date: Tue, 28 Jan 2025 18:51:08 +0100
The following T* => void* => U* => void* => T* Doesn't hold only in one case, when sizeof(U*) != sizeof(T*),Which makes the conversion truncates bytes to fit the value inside the smaller size.(like from int to short)But it is always deterministic to guess the final value from the original value, that is to say , there is no manipulation of individual bits, especially the lowest ones.So either you lose bytes or gain bytes.But so far, the above conversion chain is perfectly valid, and in this thread they want to make it standard.
-------- Original message --------From: Andrey Semashev via Std-Proposals <std-proposals_at_[hidden]> Date: 1/28/25 5:52 PM (GMT+01:00) To: std-proposals_at_[hidden] Cc: Andrey Semashev <andrey.semashev_at_[hidden]> Subject: Re: [std-proposals] Make all data pointers intercompatible On 1/28/25 18:16, organicoman via Std-Proposals wrote:> The resulting pointer is unspecified: means dereferencing it is UB.My reading of "pointer value is unspecified" is that, quite literally,the value of the pointer is unspecified (i.e. unknown). Not necessarilythe same or unchanged, as you suggest, but unknown. The consequences ofdereferencing it only follow from that.And to be clear, while this is ok:short* p1 = new short();void* p2 = static_cast< void* >(p1);short* p3 = static_cast< short* >(p2);assert(p1 == p3);this is not:short* p1 = new short();void* p2 = static_cast< void* >(p1);double* p3 = static_cast< double* >(p2);void* p4 = static_cast< void* >(p3);short* p5 = static_cast< short* >(p4);assert(p1 == p5); // allowed to failOnly the conversion from T* to void* is guaranteed to not change thepointer value. It is not guaranteed in the other direction. See[conv.ptr]/2.> 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_at_[hidden]>> Date: 1/28/25 3:40 PM (GMT+01:00)> To: std-proposals_at_[hidden]> Cc: Sebastian Wittmeier <wittmeier_at_[hidden]>> Subject: Re: [std-proposals] Make all data pointers intercompatible> > According to CPP Reference this is currently not okay:> > "If expression represents the address <https://en.cppreference.com/w/> cpp/language/pointer#Pointers> |A| of a byte <https://> en.cppreference.com/w/cpp/language/memory_model#Byte> in memory but |A|> does not satisfy the alignment <https://en.cppreference.com/w/cpp/> language/object#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_at_[hidden]>> 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));> > -- Std-Proposals mailing listStd-Proposals_at_[hidden]://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
-------- Original message --------From: Andrey Semashev via Std-Proposals <std-proposals_at_[hidden]> Date: 1/28/25 5:52 PM (GMT+01:00) To: std-proposals_at_[hidden] Cc: Andrey Semashev <andrey.semashev_at_[hidden]> Subject: Re: [std-proposals] Make all data pointers intercompatible On 1/28/25 18:16, organicoman via Std-Proposals wrote:> The resulting pointer is unspecified: means dereferencing it is UB.My reading of "pointer value is unspecified" is that, quite literally,the value of the pointer is unspecified (i.e. unknown). Not necessarilythe same or unchanged, as you suggest, but unknown. The consequences ofdereferencing it only follow from that.And to be clear, while this is ok:short* p1 = new short();void* p2 = static_cast< void* >(p1);short* p3 = static_cast< short* >(p2);assert(p1 == p3);this is not:short* p1 = new short();void* p2 = static_cast< void* >(p1);double* p3 = static_cast< double* >(p2);void* p4 = static_cast< void* >(p3);short* p5 = static_cast< short* >(p4);assert(p1 == p5); // allowed to failOnly the conversion from T* to void* is guaranteed to not change thepointer value. It is not guaranteed in the other direction. See[conv.ptr]/2.> 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_at_[hidden]>> Date: 1/28/25 3:40 PM (GMT+01:00)> To: std-proposals_at_[hidden]> Cc: Sebastian Wittmeier <wittmeier_at_[hidden]>> Subject: Re: [std-proposals] Make all data pointers intercompatible> > According to CPP Reference this is currently not okay:> > "If expression represents the address <https://en.cppreference.com/w/> cpp/language/pointer#Pointers> |A| of a byte <https://> en.cppreference.com/w/cpp/language/memory_model#Byte> in memory but |A|> does not satisfy the alignment <https://en.cppreference.com/w/cpp/> language/object#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_at_[hidden]>> 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));> > -- Std-Proposals mailing listStd-Proposals_at_[hidden]://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-01-28 17:51:15