Re: Struct compares/copies




<bb@xxxxxxx> ha scritto nel messaggio
news:s618731ejdtvf2rm0p8ipj6ll0dhsdfitr@xxxxxxxxxx
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).

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).

Even if there is no padding, the structures

struct words {
char word1[8] = { 'H', 'e', 'l', 'l', 'o', '\0', 42, 73 }
char word2[8] = { 'w', 'o', 'r', 'l,' 'd', '\0', 1, 95 }
} a;

and

struct words {
char word1[8] = { 'H', 'e', 'l', 'l', 'o', '\0', 12, 23 }
char word2[8] = { 'w', 'o', 'r', 'l,' 'd', '\0', 47, 79 }
} b;

don't memcmp equal, even if any reasonable person shouldn't
distinguish them in most cases.


.



Relevant Pages

  • 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)
  • 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 ... Well I have an odd situation because it has worked for me. ...
    (comp.lang.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: Unix C programming for finding file
    ... >> to memory map the two files and then call memcmp(). ... You can only avoid loading all files into memory if you compare ... After that test you still have to load the remaining files to be ...
    (comp.unix.programmer)
  • Re: Unix C programming for finding file
    ... I wonder if there is any library function that can compare two ... but you can use memcmp(). ...
    (comp.unix.programmer)