Deutsch English Français Italiano |
<vv6ng8$1410m$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: James Kuyper <jameskuyper@alumni.caltech.edu> Newsgroups: comp.lang.c Subject: Re: Regarding assignment to struct Date: Sat, 3 May 2025 23:38:47 -0400 Organization: A noiseless patient Spider Lines: 39 Message-ID: <vv6ng8$1410m$1@dont-email.me> References: <vv338b$16oam$1@dont-email.me> <vv4j9p$33vhj$1@dont-email.me> <vv66cu$hbtf$1@dont-email.me> <87ikmhp5x3.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 04 May 2025 05:38:50 +0200 (CEST) Injection-Info: dont-email.me; posting-host="ecc13d68c43bcf0fe88471e27b67e00a"; logging-data="1180694"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qYxPl43GwXnd9beEEPocmbKwvgbJObgY=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:B6nMklkvY36EWNKr5TS7sCH1zww= In-Reply-To: <87ikmhp5x3.fsf@nosuchdomain.example.com> Content-Language: en-US Bytes: 2947 On 5/3/25 20:37, Keith Thompson wrote: > Lawrence D'Oliveiro <ldo@nz.invalid> writes: >> On Sat, 3 May 2025 01:14:46 -0700, Andrey Tarasevich wrote: >>> Virtually every C project relies on assignment of structures. >>> Passing-returning structs by value might be more rare (although >>> perfectly valid and often appropriate too), but assignment... >>> assignment is used by everyone everywhere without even giving it a >>> second thought. >> >> There is a caveat, to do with alignment padding: will this always have a >> defined value? > > I don't believe so. In a quick look, I don't see anything in > the standard that explicitly addresses this, but I believe that a > conforming implementation could implement structure assignment by > copying the individual members, leaving any padding in the target > undefined. "When a value is stored in an object of structure or union type, including in a member object, the bytes of the object representation that correspond to any padding bytes take unspecified values.56)" (6.2.6.1p6). That refers to footnote 56, which says "Thus, for example, structure assignment need not copy any padding bits." Note that, even when writing to a single member, the representations in the padding bytes might be affected. A plausible reason for this to happen would be, for example when a value is written to an 8-bit strujct field followed by 8 bits of padding on a machine where the word size is 16 bits. The wording of that clause permits the use of instructions that change the contents of an entire word to be used when updating that field. > Finally, why would you care? The fact that an implementation does not have to do the equivalent of memcpy() to perform a struct copy means that successful assignment cannot be checked by using memcmp().