Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Preprocessor expressions Date: Tue, 07 Feb 2017 14:52:26 -0800 Organization: None to speak of Lines: 44 Message-ID: References: <53636d82-e300-4745-9cf3-b4848b97d8b9@googlegroups.com> <7a6cc8a1-6241-486e-bd87-43a0ccbe78b6@googlegroups.com> <634d154c-9d22-4a83-8d3c-85ca4b89ae06@googlegroups.com> <4bb1dbcd-6e3c-40df-8319-eb33b8bfa685@googlegroups.com> <030e3613-d745-46c4-8a46-d625eb276baa@googlegroups.com> <1e988be1-ec21-4335-8f79-e6d1e213f1c7@googlegroups.com> <86f1f0c5-5f44-4bab-8cd2-074b407220e8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="04056ce4c9c7dd85ee7ce897aab9f52a"; logging-data="23918"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19b52sgRJmTeqMVbbTLxIcH" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:p5O3SL53NmQwqEtRbr39SJnlkAk= sha1:lo8CgLPlVp566saFuITykzArjpQ= Xref: csiph.com comp.lang.c:103423 BartC writes: [...] > I had once a project which had a struct with this layout: > > struct { > int a; // 32 bits > double b; // 64 bits > int c; // 32 bits > } > > (The real thing was a lot more complex with anonymous unions everywhere. > But it had carefully crafted to occupy 16 bytes in total.) > > On x86-32, the compiler was quite happy to put member 'b' at offset 4. > > But when I compiled it for 32-bit ARM, the same compiler, gcc, had to > put 'b' at offset 8, which made the struct an odd size like 20 bytes. A size of 20 bytes seems unlikely. If the compiler imposes 8-byte alignment on type double, then it should also impose 8-byte alignment on the structure. The structure was probably 24 bytes. > This is why I think you can't always predict what a compiler will do. 99% of the time you don't have to. Usually you don't need to *tell* the compiler how things are allocated, you can just *ask* it, using sizeof, offsetof, and possibly _Alignof (which is new in C11). > (In the above example, I had to rearrange things so that 64-bit values > were at 64-bit offsets, which was probably better, but there was nothing > wrong with the original alignment that I could see. It didn't make the > x86 version any faster.) I presume you have valid reasons for wanting a particular layout. (No sarcasm there, I really do presume you have valid reasons. I don't particularly need to know what they are.) [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"