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: Sun, 11 Aug 2019 13:07:53 -0700 Organization: None to speak of Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="cb4b2dc0baf4fefc9628bc1b5f7f50df"; logging-data="17775"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bJbkHLhcEaaIeVorpiYcV" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:v8EBkqrF81J6FiyzGyyQ8Yc5Xhc= sha1:u6P0dZ2Ss3JPfno+6PW5uz0eGN8= Xref: csiph.com comp.lang.c++:67569 Bart writes: [...] > On one simple C compiler I use (actually, my own) '#pragma pack(1)' > simply disables the padding that would otherwise be inserted to ensure > proper alignment. > > It doesn't consider the legality of such accesses, although the whole > compiler does assume the target is byte-addressable (offsets of struct > members are byte-offsets from the start). And what happens if a program accesses a misaligned member? #pragma pack(1) struct foo { char c; int i; } obj = { 0 }; printf("%d\n", obj.i); If your compiler targets x86 and/or x86_64, such an access will probably just work. On other targets, it might blow up -- and if so, I would seriously question the wisdom of claiming to support #pragma pack(1). -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */