On 03/03/23 02:03, Keith Thompson wrote:
> MarioCPPP <
NoliMihiFran...@libero.it> writes:
>> On 02/03/23 18:44, Andrey Tarasevich wrote:
>>> On 03/02/23 8:25 AM, MarioCPPP wrote:
>>>>
>>>> I have this situation :
>>>>
>>>> int32_t * Pointer32; // is a 4 byte signed int
>>>> int8_t * Pointer8; // is a "signed char"
>>>> // pointer allegedly "unaligned".
>>>>
>>>> Pointer32 = new int32_t [10];
>>>> // supposedly ALIGNED a multiple of four byte addresses
>>>>
>>>> Pointer8 = static_cast <int8_t *> (Pointer32);
>>>>
>>>> ERROR : STATIC CAST FROM int * to signed char * IS NOT ALLOWED.
>>>>
>>>> I am asking why that ?
>>>>
>>>> I mean, the reverse, maybe with a warning, seems weird (at best
>>>> very slow).
>>>>
>>>> But casting a MORE RESTRICTIVELY ALIGNED pointer to one LESS
>>>> RESTRICTIVELY ALIGNED seems pretty reasonable. Why not ?
>>> "Aligned"? Why are you even mentioning alignment in this
>>> context? What does it have to do with anything?
>>
>> because pointers should grant that pointed integral types pointed are
>> accessible at correct memory addresses ... Am I wrong ?
>
> What you appear to be wrong about is what "static_cast" means.
>
>
cppreference.com is not definitive, but it tends to be pretty good.
>
>
https://en.cppreference.com/w/cpp/language/static_cast
I'll follow your advice and restudy this keyword.
Simply put I just believed that it was a cast made at
compile time plus some "checks" it was a legit cast. But
maybe this is wrong and I will restudy it !
Reinterpret_cast was new to me so I have to start from zero.
Tnx
>
>> I knew that "short" are aligned at addresses multiple of 2, "int" are
>> aligned at addresses multiple of 4, "long" are aligned at addresses
>> multiple of 8 bytes.
>
> If you mean that you know that's the case for the implementation you're
> using, that's fine. If you mean that it's true in general, it isn't.
> Different implementations can and do have different sizes and alignments
> for integer types.
it is true for types used by QTcreator that has "explicit"
fixed byte-width types for 1,2,4,8 bytes.
They are likely some typedefs defined in math.h
>
> [...]