Re: Struct compares/copies
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sat, 16 Jun 2007 09:57:59 -0700
On Sat, 16 Jun 2007 16:09:18 GMT, bb@xxxxxxx wrote:
sniipQ: I need to copy and compare C Structs.
Is this the safe and quick way to do it?:
As the comapre (new == old) made the compiler unhappy I didn't want to
Copying is easy: new = old;
trust the assign (old = new).
Trust has nothing to do with it. It is a question of what
capabilities the language provides. If you don't have a decent
reference manual, get one.
Well once I've found a change I do have to go through each field to
For safe and meaningful comparison, compare on a field-by-field basis,
comparing the most significant fields first. Typically one would write
a function to do this.
and action the changes. However the changes are rare, so I wanted to
get the 'compare' over and done with asap, hence the 'old == new' as
there can be quite a few fields in the structure (20+ at the mo).
It is not sweet -- the advice is wrong (except in the oddest ofWell I have an odd situation because it has worked for me. Can you (if
situations). It is entirely possible for memcmp to return non-zero
when the two structures are equal in all important respects (i.e. have
identical values in all members).
you have the time and inclination) tell me why memcmp would do this
(is it because of 'structure' padding/alignment and that two
'identical' structs not being identical)? I was concerned (hence the
question in the 1st place).
Padding is one situation where equal structures can have mismatched
bits. Pointers are another - it is possible for two pointers to the
same address to have different bit representations. A system that
supports -0 is another. 0 and -0 must compare equal but will not have
the same bit representation. There probably are other situations
also.
Remove del for email
.
- References:
- Struct compares/copies
- From: bb
- Re: Struct compares/copies
- From: Richard Heathfield
- Re: Struct compares/copies
- From: bb
- Struct compares/copies
- Prev by Date: Re: sorting/merging in C
- Next by Date: Re: Struct compares/copies
- Previous by thread: Re: Struct compares/copies
- Next by thread: Re: Struct compares/copies
- Index(es):
Relevant Pages
|