Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Performance of unaligned memory-accesses Date: Sat, 10 Aug 2019 17:32:53 -0700 Organization: None to speak of Lines: 41 Message-ID: References: <52b7afac-e701-40d9-8257-d9cfa7c51243@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="cb4b2dc0baf4fefc9628bc1b5f7f50df"; logging-data="27227"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mvNPj2sg7B0Wo1qeIZj8P" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:sTFxod8yd4rCwn2zF3Ss4BeGZAc= sha1:BwlpSoD28b2Ow1woXtlMImnRRgM= Xref: csiph.com comp.lang.c++:67546 James Kuyper writes: > On Saturday, August 10, 2019 at 6:23:16 PM UTC-4, Bart wrote: >> On 10/08/2019 22:46, David Brown wrote: > ... >> > 6.3.2.3p7: >> > """ >> > A pointer to an object type may be converted to a pointer to a different >> > object type. If the resulting pointer is not correctly aligned 68) for >> > the referenced type, the behavior is undefined. >> > """ >> >> What does 'correctly aligned' mean? > > That's implementation-defined (6.2.8p1). In C2011 you can determine the > alignment of a given type under a given implementation by using > _Alignof(typename). There is a possibly useful distinction that this doesn't capture. For example, on my x86_64 system, gcc has sizeof (int) == 4 and _Alignof (int) == 4. The compiler will, in the absence of implementation-specific directives, align int objects at 4-byte boundaries, and any attempt to access an int object that's not 4-byte aligned has undefined behavior. But you can successfully access int objects at odd addresses, though doing so is likely to be less efficient (but probably more efficient than accessing it a byte at a time). (Possibly compiler optimizations might result in surprising and misbehaving code in such cases.) Would it be useful to be able to query both the alignment at which objects will be allocated and the (possibly smaller) alignment at which objects can be accessed without going kaboom? [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */