473,382 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

comparing two structures

Guys,

Is it a good way to compare two structures for equality by using
"memcmp" ?

If not, what could be the problems associated with this ? And what is
the correct
method of doing this ?

thanks for any help.

Sep 14 '07 #1
17 8604
ju**********@yahoo.co.in said:
Guys,

Is it a good way to compare two structures for equality by using
"memcmp" ?
No.
If not, what could be the problems associated with this ?
Padding bytes, and dynamically allocated memory pointed to by struct
members.
And what is the correct method of doing this ?
int cmp(const struct T *a, const struct T *b);

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 14 '07 #2
<ju**********@yahoo.co.inschrieb im Newsbeitrag
news:11*********************@w3g2000hsg.googlegrou ps.com...
Guys,

Is it a good way to compare two structures for equality by using
"memcmp" ?

If not, what could be the problems associated with this ? And what is
the correct
method of doing this ?

thanks for any help.
http://c-faq.com/struct/compare.html
Sep 14 '07 #3
Richard Heathfield wrote:
ju**********@yahoo.co.in said:
>Is it a good way to compare two structures for equality by using
"memcmp" ?

No.
>If not, what could be the problems associated with this ?

Padding bytes, and dynamically allocated memory pointed to by
struct members.
>And what is the correct method of doing this ?

int cmp(const struct T *a, const struct T *b);
Which should resolve to (very roughly}:

for (c = eachcomponentof a) {
if ((a->c) != (b->c)) return 0; /* unequal */
}
return 1; /* i.e. structures equal */

Which means you have to write a cmp() each structure type. You may
have to resolve embedded pointers.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 14 '07 #4
CBFalconer said:
Richard Heathfield wrote:
<snip>
>>
int cmp(const struct T *a, const struct T *b);

Which should resolve to (very roughly}:

for (c = eachcomponentof a) {
if ((a->c) != (b->c)) return 0; /* unequal */
}
return 1; /* i.e. structures equal */
Personally, I would find a relational comparison more generally useful.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 14 '07 #5
In <11*********************@w3g2000hsg.googlegroups.c om"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:
Is it a good way to compare two structures for equality by using
"memcmp" ?
No.
If not, what could be the problems associated with this?
One such problem is comparison of strings. The contents of a string are
meaningless after the terminating null byte, but memcmp() wouldn't know
that.

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Sep 14 '07 #6
CBFalconer wrote:

[...]
Which should resolve to (very roughly}:

for (c = eachcomponentof a) {
if ((a->c) != (b->c)) return 0; /* unequal */
}
return 1; /* i.e. structures equal */

Which means you have to write a cmp() each structure type. You may
have to resolve embedded pointers.
Very confusing name choice, unless cmp() return 0 if equal.

--
Tor <torust [at] online [dot] no>
Sep 14 '07 #7
Richard Heathfield wrote:
CBFalconer said:
>Richard Heathfield wrote:

<snip>
>>>
int cmp(const struct T *a, const struct T *b);

Which should resolve to (very roughly}:

for (c = eachcomponentof a) {
if ((a->c) != (b->c)) return 0; /* unequal */
}
return 1; /* i.e. structures equal */

Personally, I would find a relational comparison more generally
useful.
Granted, but generally impossible. For:

struct foobar {int foo; char bar);

please define a suitable cmp function that is relational, without
somehow defining the relative importance of foo and bar. :-) I'm
sure you know this, but the comment is for general consumption.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

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

Sep 14 '07 #8
Tor Rustad wrote:
CBFalconer wrote:

[...]
>Which should resolve to (very roughly}:

for (c = eachcomponentof a) {
if ((a->c) != (b->c)) return 0; /* unequal */
}
return 1; /* i.e. structures equal */

Which means you have to write a cmp() each structure type. You
may have to resolve embedded pointers.

Very confusing name choice, unless cmp() return 0 if equal.
You have my full permission to interchange 0 and 1. :-)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

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

Sep 14 '07 #9
CBFalconer wrote:
Tor Rustad wrote:
>CBFalconer wrote:

[...]
>>Which should resolve to (very roughly}:

for (c = eachcomponentof a) {
if ((a->c) != (b->c)) return 0; /* unequal */
}
return 1; /* i.e. structures equal */

Which means you have to write a cmp() each structure type. You
may have to resolve embedded pointers.
Very confusing name choice, unless cmp() return 0 if equal.

You have my full permission to interchange 0 and 1. :-)
The API is still broken to my mind. :)

If first argument is less, then cmp() should return negative (e.g. -1),
if first argument is greater, then cmp() should return positive (e.g. 1).
Furthermore, if writing a cmp() function, I rather prefer to use the
same interface and return value as qsort() and bsearch() expect:

int cmp_mystruct(const void *, const void *)

why compare "struct's", unless doing sorting and searching?

--
Tor <torust [at] online [dot] no>
Sep 15 '07 #10
Tor Rustad said:

<snip>
Furthermore, if writing a cmp() function, I rather prefer to use the
same interface and return value as qsort() and bsearch() expect:

int cmp_mystruct(const void *, const void *)

why compare "struct's", unless doing sorting and searching?
Well said, although I think I'd like to add an extra void * to that, so
that the comparison function can use (or provide) additional
information about the comparison without resorting to global data.
This, of course, requires ISO to change the spec for qsort and bsearch,
or provide sensibly-designed versions thereof.

Jump to it, lads...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 15 '07 #11
Tor Rustad wrote:
Furthermore, if writing a cmp() function, I rather prefer to use the
same interface and return value as qsort() and bsearch() expect:

int cmp_mystruct(const void *, const void *)

why compare "struct's", unless doing sorting and searching?
If you had a linked list of structures
and if you had a list sorting function that
took a compar function pointer as an argument,
then it would make more sense to have pointers to the list node type.

--
pete
Sep 15 '07 #12
Tor Rustad wrote:
CBFalconer wrote:
>Tor Rustad wrote:
>>CBFalconer wrote:

[...]

Which should resolve to (very roughly}:

for (c = eachcomponentof a) {
if ((a->c) != (b->c)) return 0; /* unequal */
}
return 1; /* i.e. structures equal */

Which means you have to write a cmp() each structure type.
You may have to resolve embedded pointers.

Very confusing name choice, unless cmp() return 0 if equal.

You have my full permission to interchange 0 and 1. :-)

The API is still broken to my mind. :)

If first argument is less, then cmp() should return negative
(e.g. -1), if first argument is greater, then cmp() should
return positive (e.g. 1).
But you are dealing with a struct. How do you define 'less' when
there are multiple components, with no less/greater definition.
>
Furthermore, if writing a cmp() function, I rather prefer to use
the same interface and return value as qsort() and bsearch()
expect:

int cmp_mystruct(const void *, const void *)

why compare "struct's", unless doing sorting and searching?
No, you can compare some fields of structs, but not complete
structs, for relative magnitude. The problem handled by void* is
not within the cmp function, but within the caller, who may not
need to know anything more than the cmp result about the struct.
The definition of cmp is up to you.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

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

Sep 15 '07 #13
CBFalconer said:

<snip>
But you are dealing with a struct. How do you define 'less' when
there are multiple components, with no less/greater definition.
It depends on the domain. You are right that there is no general
solution, but that does not mean that there are no domain-specific
solutions. Nor does it mean that you cannot have multiple comparison
functions for the same type.

<snip>
The definition of cmp is up to you.
Precisely.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 15 '07 #14
CBFalconer wrote:
Tor Rustad wrote:
>CBFalconer wrote:
>>Tor Rustad wrote:
CBFalconer wrote:

[...]

Which should resolve to (very roughly}:
>
for (c = eachcomponentof a) {
if ((a->c) != (b->c)) return 0; /* unequal */
}
return 1; /* i.e. structures equal */
>
Which means you have to write a cmp() each structure type.
You may have to resolve embedded pointers.
Very confusing name choice, unless cmp() return 0 if equal.
You have my full permission to interchange 0 and 1. :-)
The API is still broken to my mind. :)

If first argument is less, then cmp() should return negative
(e.g. -1), if first argument is greater, then cmp() should
return positive (e.g. 1).

But you are dealing with a struct. How do you define 'less' when
there are multiple components, with no less/greater definition.
Hmmm.. what does this "comparing struct" mean?

Normally, when comparing ADT, the ADT has a key defined, e.g. like this:

struct node
{
KEY_T id;
DATA_T data;
};

and you only compare the key, not *all* the members of the struct.

When a programmer name a function cmp(), I expect a compare operation to
be defined. If only testing for equality, a far better name choice would
be e.g. 'is_equal()'.

See return value of memcmp, strcmp, ... it's <0, 0 or >0.

--
Tor <torust [at] online [dot] no>
Sep 16 '07 #15
pete wrote:
Tor Rustad wrote:
>Furthermore, if writing a cmp() function, I rather prefer to use the
same interface and return value as qsort() and bsearch() expect:

int cmp_mystruct(const void *, const void *)

why compare "struct's", unless doing sorting and searching?

If you had a linked list of structures
and if you had a list sorting function that
took a compar function pointer as an argument,
then it would make more sense to have pointers to the list node type.
Agreed, in this case, I wouldn't use void*, but rather

int is_equal(KEY_T, KEY_T );
int is_less (KEY_T, KEY_T );

--
Tor <torust [at] online [dot] no>
Sep 16 '07 #16
Richard Heathfield wrote:
CBFalconer said:
[...]
>The definition of cmp is up to you.

Precisely.
The compiler doesn't mind bad identifiers, but humans do...

The point is that the standard C library, the *is* functions return
differently, than the *cmp* functions do.
So, if you are consistent, you can't object to code with:

int is_zero(int c)
{
return ('0' != c);
}

either.

--
Tor <torust [at] online [dot] no>
Sep 16 '07 #17
Tor Rustad said:
Richard Heathfield wrote:
>CBFalconer said:

[...]
>>The definition of cmp is up to you.

Precisely.

The compiler doesn't mind bad identifiers, but humans do...

The point is that the standard C library, the *is* functions return
differently, than the *cmp* functions do.
So, if you are consistent, you can't object to code with:

int is_zero(int c)
{
return ('0' != c);
}

either.

You lost me. Remember that my preference is for relational comparison, a
la strcmp and memcmp, rather than for equality-only comparison.

If you realised this and still think you have a point, I'd be curious to
know what that point is.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 16 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Doug | last post by:
I need to compare two "address" structures within a document, and perform some action if they are not equal. The XML document is a purchase order, with an address at both the header and line...
3
by: Christophe Rhin | last post by:
I have two versions of the same C++ framework (> 200 000 lines). These two versions evolved apart during 2 years from the same starting point. My goal is two find "significant" differences between...
11
by: Jeff | last post by:
Hi - I'm experiencing a strange problem when comparing 2 guids. In my trial, they're not equal. When I step through the (VB.NET) code, they are evaluated as equal, and when I enter the...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
9
by: fripper | last post by:
I have a simple VB 2005 app that has a bitmap image and at one point I want to look at a particular pixel and see if it has a color value equal to the value of a color variable, c. For example: ...
12
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
20
by: subramanian100in | last post by:
Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, of data. Consider the structures: struct struct_one { TYPE1 data1; TYPE2 data2; TYPE3 data3;
6
by: gunjan29484 | last post by:
Hello, i want to compare the two directory structures and find out if any file or directory is missing in the second directory structure or not. (directory1 is a super set of directory2) I have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.