Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: strange error (QT Creator 4.12.2, based on QT creator 5.14.2) Date: Thu, 02 Mar 2023 17:03:16 -0800 Organization: None to speak of Lines: 51 Message-ID: <87ilfimysr.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="faddc2c373036d68cc5ec870139a1e06"; logging-data="469351"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Z/9nOriMl2NQZHXErWIz3" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:++GeQwOQylji/U5B87bmL/rqytI= sha1:4KCPR7M6F1GWLOgP4054PW4vZiI= Xref: csiph.com comp.lang.c++:89402 MarioCPPP 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 (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 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. [...] -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for XCOM Labs void Void(void) { Void(); } /* The recursive call of the void */