Deutsch   English   Français   Italiano  
<vv85jc$2c9b3$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Regarding assignment to struct
Date: Sun, 4 May 2025 18:45:32 +0200
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <vv85jc$2c9b3$2@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>
 <vv6ng8$1410m$1@dont-email.me> <VOKRP.167848$7IN2.2401@fx15.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 04 May 2025 18:45:44 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a67104135396b8e53108b98b5d72143e";
	logging-data="2499939"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19vsuNTewoYOWt84PWmNgiReux9UJZ5Yho="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:4wvv/ArOKbF5v1cUVTNi3XYTE+I=
In-Reply-To: <VOKRP.167848$7IN2.2401@fx15.iad>
Content-Language: en-GB

On 04/05/2025 16:27, Scott Lurndal wrote:
> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>> 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."
> 
> Are there any C implementations in common use that don't just
> use memcpy or an optimized version thereof?
> 

Sometimes small structs never make it to memory, or are handled by the 
compiler as though they were individual variables (as long as that is 
within "as-if" usage, of course).  Copying a struct might merely mean 
the compiler keeps track of the logical copy without actually copying 
any memory.  (You could argue that the compiler is still treating it 
like memcpy, as memcpy calls don't always copy something.)

I think it would be unusual to see a significant difference between a 
struct assignment copy and a memcpy on a compiler that optimises memcpy 
well.

But on a compiler that does not handle memcpy well, then a struct 
assignment could be inlined while a memcpy could mean an external 
library call with significant overhead.