Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Struct compares/copies

Reply
Thread Tools

Struct compares/copies

 
 
bb@c.co.uk
Guest
Posts: n/a
 
      06-16-2007
Q: I need to copy and compare C Structs.

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

void func(void)
{
typedef _MY_STRUCT
{
int a;
char b;
char c;
} my_struct

my_stuct new;
my_struct old;

if (memcmp(old,new,sizeof(new))
{
memcpy(old, new, sizeof(new));
}
}
 
Reply With Quote
 
 
 
 
bb@c.co.uk
Guest
Posts: n/a
 
      06-16-2007
On Sat, 16 Jun 2007 14:01:25 GMT, wrote:

>Q: I need to copy and compare C Structs.
>
>Is this the safe and quick way to do it?:
>
>void func(void)
>{
>typedef struct _MY_STRUCT
>{
> int a;
> char b;
> char c;
>} my_struct;
>
>my_stuct new;
>my_struct old;
>
> if (memcmp(old,new,sizeof(new))
> {
> memcpy(old, new, sizeof(new));
> }
>}


typos corrected!
 
Reply With Quote
 
 
 
 
deepak
Guest
Posts: n/a
 
      06-16-2007
On Jun 16, 7:01 pm, b...@c.co.uk wrote:
> Q: I need to copy and compare C Structs.
>
> Is this the safe and quick way to do it?:
>
> void func(void)
> {
> typedef _MY_STRUCT
> {
> int a;
> char b;
> char c;
>
> } my_struct
>
> my_stuct new;
> my_struct old;
>
> if (memcmp(old,new,sizeof(new))
> {
> memcpy(old, new, sizeof(new));
> }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -


I think the use of memcmp in this code will be like,
if (!memcmp(old,new,sizeof(new))
{
memcpy(old, new, sizeof(new));
}

If the content is same why you need to copy it again?
Else for me code is ok.

 
Reply With Quote
 
bb@c.co.uk
Guest
Posts: n/a
 
      06-16-2007
On Sat, 16 Jun 2007 14:12:48 -0000, deepak <>
wrote:

>On Jun 16, 7:01 pm, b...@c.co.uk wrote:
>> Q: I need to copy and compare C Structs.
>>
>> Is this the safe and quick way to do it?:
>>
>> void func(void)
>> {
>> typedef _MY_STRUCT
>> {
>> int a;
>> char b;
>> char c;
>>
>> } my_struct
>>
>> my_stuct new;
>> my_struct old;
>>
>> if (memcmp(old,new,sizeof(new))
>> {
>> memcpy(old, new, sizeof(new));
>> }
>>
>>
>>
>> }- Hide quoted text -
>>
>> - Show quoted text -

>
>I think the use of memcmp in this code will be like,
>if (!memcmp(old,new,sizeof(new))
>{
> memcpy(old, new, sizeof(new));
>}
>
>If the content is same why you need to copy it again?

Sorry, yes memcmp returns 0 if match. Typo (another one).
>Else for me code is ok.

Sweet. Thank-you.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-16-2007
said:

> Q: I need to copy and compare C Structs.
>
> Is this the safe and quick way to do it?:


Copying is easy: new = old;

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-16-2007
writes:

> On Sat, 16 Jun 2007 14:12:48 -0000, deepak <>
> wrote:
>
>>On Jun 16, 7:01 pm, b...@c.co.uk wrote:
>>> Q: I need to copy and compare C Structs.
>>>
>>> Is this the safe and quick way to do it?:
>>>
>>> void func(void)
>>> {
>>> typedef _MY_STRUCT
>>> {
>>> int a;
>>> char b;
>>> char c;
>>>
>>> } my_struct
>>>
>>> my_stuct new;
>>> my_struct old;

<snip>
>>I think the use of memcmp in this code will be like,
>>if (!memcmp(old,new,sizeof(new))
>>{
>> memcpy(old, new, sizeof(new));
>>}
>>
>>If the content is same why you need to copy it again?

> Sorry, yes memcmp returns 0 if match. Typo (another one).
>>Else for me code is ok.

> Sweet. Thank-you.


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

--
Ben.
 
Reply With Quote
 
Clark Cox
Guest
Posts: n/a
 
      06-16-2007
On 2007-06-16 07:01:25 -0700, said:

> Q: I need to copy and compare C Structs.
>
> Is this the safe and quick way to do it?:


[snip memcpy/memcmp-using code]


No. For copying, you can use the '=' operator in the obvious way:
old = new;

For comparing, you'll have to do that yourself, member-by-member. Using
memcmp isn't a good idea because it doesn't take into account any
padding that may be contained in the structure's layout (i.e. you could
actually be comparing bytes that don't contribute in any meaningful way
to the structure's value).


--
Clark S. Cox III


 
Reply With Quote
 
bb@c.co.uk
Guest
Posts: n/a
 
      06-16-2007
>> 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).
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-16-2007
writes:

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


Yes, the problem is (mostly) structure padding. If you are certain
the there is none (on all the platforms you can foresee) or that you
have set to some known value in every case (because you have used
memset always) then you might me able to get away with it, but it
seems to me you would be making a fragile solution.

There is another sort of padding internal to some types that the
standard allows. Hence one can not assume that two values that
compare equal will be equal as far as memcmp is concerned.

Finally, some types have their own notion of equality that need not be
byte-for-byte equality. This is most obvious for floating point types
but it is also possible that two pointers might be == but have distinct
representations. Similarly, when a type permits trap representations,
the effect of == and memcmp may be different (but in this case the
memcmp will succeed in a possibly deceptive way).

BTW, it is probably not a good idea to join two answers together like
this. For one thing you can't easily keep the attribution lines (you
should have kept them, in my opinion).

--
Ben.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-16-2007
Ben Bacarisse wrote:
>

.... snip on comparing structs ...
>
> 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).


The reason is that there can be padding bytes, which contain data
that has nothing to do with the value of the struct. You have to
compare field by field.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
struct in struct Gunnar G C++ 14 06-02-2004 06:43 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57