C++ Logo

std-proposals

Advanced search

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

From: organicoman <organicoman_at_[hidden]>
Date: Tue, 28 Jan 2025 11:12:53 +0100
Frederick, sizeof(void*) >= sizeof(char*) sizeof(void*) >= sizeof( any class pointer )This is more accurate than equality. void* should have a large enough size to hold the representation of any pointer value without alteration.Also if for expl:sizeof(char*) != sizeof(T*)Strict aliasing rule mandate that to be able to cast between these two types safelyyou must fulfill 3 conditions:1- sizeof(T*) >= sizeof(char*)2- T and char are compatible types3- no const violation.Compatible types means, the data is aligned at a memory boundary where the value can be read naturally.So if you are in doubt if two pointer types U and T are not the same size then it should be safe to add an intermediary cast to void* before casting to the target pointer type.T* t;void* v = static_cast<void*>(t);U* u = static_cast<U*>(v);But dereferencing the pointer is UB, unless the 3 conditions above are met.Regards Sent from my Galaxy
-------- Original message --------From: Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Date: 1/28/25 10:42 AM (GMT+01:00) To: std-proposals_at_[hidden] Cc: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]> Subject: Re: [std-proposals] Make all data pointers intercompatible Not entirely sure where this thread has ventured off to, but let meget back to what I was talking about (i.e. the size of pointers).Given the following translation unit: class MyClass; MyClass *my_global_variable;The compiler doesn't know the alignment requirements of MyClass. Forinstance it might be: struct MyClass { char c; };or it could be: struct MyClass { long double f; };Therefore I think we can make the following assertion: sizeof(void*) == sizeof(char*) == sizeof( any class pointer )This means that we should be able to do the following: class SomeClass; void *GiveMeBackTheArgumentUnchanged(void *const arg) { SomeClass *const p = static_cast<SomeClass*>(arg); return p; }The above code should work fine on every C++ compiler because aSomeClass* should be the same as a void*.The only complication though is that the following pointers couldstill be smaller than a void*:    short*    int*    long*    long long*    float*    double* long double*But since there's no compiler alive today that actually does that, Ithink C++26 should mandate that all data pointers are the same sizeand representation.-- Std-Proposals mailing listStd-Proposals_at_[hidden]://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-01-28 10:12:59