Author: Heiner Marxen
Date: 07:03:46 05/13/03
Go up one level in this thread
On May 13, 2003 at 04:54:58, Tom Kerrigan wrote: >On May 12, 2003 at 14:49:21, Dieter Buerssner wrote: > >>On May 12, 2003 at 14:43:00, Tom Kerrigan wrote: >> >>>On May 12, 2003 at 10:38:57, Fermin Serrano wrote: >>>> Question 3: >>>> ----------------- >>>> To copy a structure into other, what is faster, doing by copy each of his >>>>elements or use a memcpy? >>> >>>Use memcpy. It may not be faster on your system, but it's optimized on other >>>systems that you might use later. If you copy individual elements yourself, >>>there's no chance it will be optimized on any system. >> >>I don't agree. Use the assignment operator ("=")! It should never do worse than >>memcpy or copying individual members. On many compilers, it will make no I second that. >You're probably right, assuming your compiler supports copying structures. I >recall that some don't, but I imagine most current ones do. IIRC, ANSI-C requires structure assignments. I haven't met a C compiler without struct assignments for many years, now, and I've never seen an ANSI compliant one without it. Hence: if you want to copy a structure, assign it. There is only one possible pitfall with this, I'm aware of: if you later want to compare two structs with "memcmp" instead of component by component, you also compare the "alignment gaps" of the structures (if there are any). And the assignment _may_ omit copying the gaps (copying component by component). That may result in "unequal" results of memcmp(), although all components are equal. [Yes, that one has bitten me.] >-Tom Cheers, Heiner
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.