473,568 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8620
ju**********@ya hoo.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**********@y ahoo.co.inschri eb im Newsbeitrag
news:11******** *************@w 3g2000hsg.googl egroups.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**********@ya hoo.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************ *********@w3g20 00hsg.googlegro ups.com"ju***** *****@yahoo.co. in" <ju**********@y ahoo.co.inwrite s:
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.co m 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
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(co nst 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 #10

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

Similar topics

1
2965
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 item level: <Order> <Header> ... other header level stuff ... <Address>
3
1414
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 these two versions. A diff tool (like WinMerge, excellent) produces too many differences (spaces, new lines, comments, real changes, renaming,...
11
7244
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 comparison in the command window, they're not equal. I'm pretty stumped on this one. Please help. I've tried the following structures, all with the...
19
2632
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
9
5455
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: dim c as color c = color.red If bitmap(x,y) = c then
12
25973
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
1883
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
2253
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 written some code but i am confused with where exaclty the recursive call should be made and what all should be passed as directory name changes at...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.