Re: Struct compares/copies



On Sat, 16 Jun 2007 16:09:18 GMT, bb@xxxxxxx wrote:

Q: I need to copy and compare C Structs.

Is this the safe and quick way to do it?:
sniip


Copying is easy: new = old;
As the comapre (new == old) made the compiler unhappy I didn't want to
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.


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.
Well once I've found a change I do have to go through each field to
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 of
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).
Well I have an odd situation because it has worked for me. Can you (if
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
.



Relevant Pages

  • Re: Adjacent arrays in C99
    ... yield that those representations are unequal if they are equal and vice ... compare equal, and apparently even have the identical representations, ... same pointer "value", in the sense that they'd carry identical promises ... an incomplete type, called the referenced type. ...
    (comp.std.c)
  • Re: Adjacent arrays in C99
    ... do compare equal, they still are considered distinct values, and the ... have different object representations. ... Most implementations represent a pointer to past an array as the machine address of the next byte after the array, and most do not go through hoops to avoid allocating that byte to another object. ... In general, my feeling is that the standard is painfully lacking when it comes to the discussion of operations on pointers, particularly conversions. ...
    (comp.std.c)
  • Re: beginner web site maintainer
    ... Either use a text editor like bluefish found at http://bluefish.openoffice.nl/download.htmlto compare the pages or print out the page that works and one that does not and compare them line by line. ... Change everything in one version and upload it, review it - if it blew up, upload the safe copy and start over. ... As I see it you have two choices, buy HTML for the World Wide Web by Elisabeth Castro and Eric Meyer on CSS and get to work, or admit you are in over your head and bail out. ...
    (alt.html)
  • Re: File-Compare "fc" falsely reports mismatch between identical files
    ... said to compare files as ascii text. ... Redmond is some variety of universal standard. ... mean that there is no benefit in making the distinction between ASCII ... it is now safe to turn your computer off. ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: Struct compares/copies
    ... For safe and meaningful comparison, compare on a field-by-field basis, ... It is entirely possible for memcmp to return non-zero ... the problem is structure padding. ...
    (comp.lang.c)