Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Padding between char array/VLA in struct? Date: Wed, 04 Aug 2021 10:11:21 -0700 Organization: None to speak of Lines: 42 Message-ID: <87fsvp879i.fsf@nosuchdomain.example.com> References: <87sfzr8umf.fsf@nosuchdomain.example.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="99395c8d90a6c5103d50d5b3645d1b26"; logging-data="27407"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zQEEKvkEk59JvH7XR6OMr" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:xDUHi5YT205zbDMnB6NhO2oicdc= sha1:Mpi7ou7bGekiwmFRF3BjTqTbLk0= Xref: csiph.com comp.lang.c:162233 Manfred writes: > On 8/2/2021 10:22 PM, Keith Thompson wrote: >> Ian Pilcher writes: >>> On 8/2/21 12:48 PM, Ian Pilcher wrote: >>>> Given the following: >>>>   struct foo { >>>>       /* other members */ >>>>       char prefix[PREFIX_SIZE]; >>>>       char name[]; >>>>   }; >>> >>> As has been pointed out, the 'name' member is a flexible array member, >>> not a variable length array. I apologize for the mistake. >>> >>> Also, PREFIX_SIZE is intended to represent a macro that expands to a >>> positive(!) integer constant expression. >>> >>> The consensus seems to be that no one can think of a good reason that a >>> compiler would insert padding between the 'prefix' and 'name' members, >>> but there's nothing in any of the language standards that would forbid >>> it from doing so. That would certainly explain my failure to find any >>> such rule. >>> >>> (_Static_assert here I come!) >> The ABI for whatever target platform you're interested in might have >> something to say about it. > > It may be worth pointing out that ABIs set requirements for exported > interfaces - they don't pose restrictions to compilers for accesses to > local data and functions. Sure, but it would be bizarre to use the ABI-imposed layout for struct foo if it's exported, but a different layout if it isn't. >> (I can imagine that some compiler might find it convenient to give >> the >> name[] member an alignment stricter than 1 byte.) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */