struct comparison

I would like to compare 2 struct of the same type and determine if they are
equal, ie they have the same values in each data member. Is there a way I
can do this without comparing each data member? Sorry if the answer is
obvious and I just don’t see it, I am a bit out of it today. Thanks.

Ross

Ross Brantner <rbrantner01@charter.net> wrote:

I would like to compare 2 struct of the same type and determine if they are
equal, ie they have the same values in each data member. Is there a way I
can do this without comparing each data member? Sorry if the answer is
obvious and I just don’t see it, I am a bit out of it today. Thanks.

memcmp()?

Cheers,
-RK


\

Robert Krten, PARSE Software Devices +1 613 599 8316.
Realtime Systems Architecture, Consulting and Training at www.parse.com
Email my initials at parse dot com.

“Ross Brantner” <rbrantner01@charter.net> wrote in message
news:9u5us6$en9$1@inn.qnx.com

I would like to compare 2 struct of the same type and determine if they
are
equal, ie they have the same values in each data member. Is there a way I
can do this without comparing each data member? Sorry if the answer is
obvious and I just don’t see it, I am a bit out of it today. Thanks.

The only portable way is to compare each data member one by one.
http://www.eskimo.com/~scs/C-faq/q2.8.html

A non portable way is to use memcmp(). I have never seen a Compiler/OS
where this didn’t work though.

Ross

thank you, i had an idea that memcmp might work but figured i would ask the
experts. many thanks.


Mario Charest <mcharest@clipzinformatic.com> wrote in message
news:9u603h$fge$1@inn.qnx.com

“Ross Brantner” <> rbrantner01@charter.net> > wrote in message
news:9u5us6$en9$> 1@inn.qnx.com> …
I would like to compare 2 struct of the same type and determine if they
are
equal, ie they have the same values in each data member. Is there a way
I
can do this without comparing each data member? Sorry if the answer is
obvious and I just don’t see it, I am a bit out of it today. Thanks.


The only portable way is to compare each data member one by one.
http://www.eskimo.com/~scs/C-faq/q2.8.html

A non portable way is to use memcmp(). I have never seen a Compiler/OS
where this didn’t work though.

Ross
\

“Mario Charest” <mcharest@clipzinformatic.com> wrote in message
news:9u603h$fge$1@inn.qnx.com

“Ross Brantner” <> rbrantner01@charter.net> > wrote in message
news:9u5us6$en9$> 1@inn.qnx.com> …
I would like to compare 2 struct of the same type and determine if they
are
equal, ie they have the same values in each data member. Is there a way
I
can do this without comparing each data member? Sorry if the answer is
obvious and I just don’t see it, I am a bit out of it today. Thanks.


The only portable way is to compare each data member one by one.
http://www.eskimo.com/~scs/C-faq/q2.8.html

A non portable way is to use memcmp(). I have never seen a Compiler/OS
where this didn’t work though.

thanks Mario, this is a great and usefull link.

// wbr

I think that in a system where the data member alignment of a struct allows
padding between data members, a memcmp() could conceivably fail even though
a member by member compare would sucseed.


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


“Mario Charest” <mcharest@clipzinformatic.com> wrote in message
news:9u603h$fge$1@inn.qnx.com

“Ross Brantner” <> rbrantner01@charter.net> > wrote in message
news:9u5us6$en9$> 1@inn.qnx.com> …
I would like to compare 2 struct of the same type and determine if they
are
equal, ie they have the same values in each data member. Is there a way
I
can do this without comparing each data member? Sorry if the answer is
obvious and I just don’t see it, I am a bit out of it today. Thanks.


The only portable way is to compare each data member one by one.
http://www.eskimo.com/~scs/C-faq/q2.8.html

A non portable way is to use memcmp(). I have never seen a Compiler/OS
where this didn’t work though.

Ross
\

In article <9u76e2$8rh$1@inn.qnx.com>,
Bill Caroselli <qtps@earthlink.net> wrote:

I think that in a system where the data member alignment of a struct allows
padding between data members, a memcmp() could conceivably fail even though
a member by member compare would sucseed.

That’s right. The thing is that you can’t assume much about
the non-member space in a struct.

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

Bill Caroselli <qtps@earthlink.net> wrote:

I think that in a system where the data member alignment of a struct allows
padding between data members, a memcmp() could conceivably fail even though
a member by member compare would sucseed.

I don’t know about that – that seems to imply way too much “thinking” on
behalf of the runtime package. I mean, think about it. The compiler is going
to say “for this structure, the members are at offsets a, b, and c”. Then,
when you access the structure through a pointer, I’m sure the compiler isn’t
going to generate code that says “hey, lets first look at this pointer, and
then, at runtime, select one of N different pieces of code”. (Unless compilers have
progressed significantly). Since the compiler can’t generate/select different code
sequences to handle it at runtime, therefore the structure layouts must be
identical regardless of address, therefore memcmp must work on all reasonable
architectures.

Well, that’s if I was running the show, anyway :slight_smile:

-RK



Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net



“Mario Charest” <> mcharest@clipzinformatic.com> > wrote in message
news:9u603h$fge$> 1@inn.qnx.com> …

“Ross Brantner” <> rbrantner01@charter.net> > wrote in message
news:9u5us6$en9$> 1@inn.qnx.com> …
I would like to compare 2 struct of the same type and determine if they
are
equal, ie they have the same values in each data member. Is there a way
I
can do this without comparing each data member? Sorry if the answer is
obvious and I just don’t see it, I am a bit out of it today. Thanks.


The only portable way is to compare each data member one by one.
http://www.eskimo.com/~scs/C-faq/q2.8.html

A non portable way is to use memcmp(). I have never seen a Compiler/OS
where this didn’t work though.

Ross

\


Robert Krten, PARSE Software Devices +1 613 599 8316.
Realtime Systems Architecture, Consulting and Training at www.parse.com
Email my initials at parse dot com.

Greg Comeau <comeau@panix.com> wrote:

In article <9u76e2$8rh$> 1@inn.qnx.com> >,
Bill Caroselli <> qtps@earthlink.net> > wrote:
I think that in a system where the data member alignment of a struct allows
padding between data members, a memcmp() could conceivably fail even though
a member by member compare would sucseed.

That’s right. The thing is that you can’t assume much about
the non-member space in a struct.

Ahh… gotcha. How about if I explicitly memset all the members and padding to
zero before each use – could I then guarantee that a memcmp works? I guess
my previous post puts egg on my face :slight_smile:

-RK

Robert Krten, PARSE Software Devices +1 613 599 8316.
Realtime Systems Architecture, Consulting and Training at www.parse.com
Email my initials at parse dot com.

Hi Robert

Agreed. Offsets can’t change.

The problem is that given

struct X
{
char name[3];
int x;
};
and assuming a 4 byte alignment there will be 1 byte after name that is not
initialized. It would probibly contain whatever was in that memory before
this struct was allocated. So if 2 instances of this struct are
instanciated and initialized identically, that 1 byte may still be
different.


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net

<nospam93@parse.com> wrote in message news:9u8990$2jp$1@inn.qnx.com

Bill Caroselli <> qtps@earthlink.net> > wrote:
I think that in a system where the data member alignment of a struct
allows
padding between data members, a memcmp() could conceivably fail even
though
a member by member compare would sucseed.

I don’t know about that – that seems to imply way too much “thinking” on
behalf of the runtime package. I mean, think about it. The compiler is
going
to say “for this structure, the members are at offsets a, b, and c”.
Then,
when you access the structure through a pointer, I’m sure the compiler
isn’t
going to generate code that says “hey, lets first look at this pointer,
and
then, at runtime, select one of N different pieces of code”. (Unless
compilers have
progressed significantly). Since the compiler can’t generate/select
different code
sequences to handle it at runtime, therefore the structure layouts must be
identical regardless of address, therefore memcmp must work on all
reasonable
architectures.

Well, that’s if I was running the show, anyway > :slight_smile:

-RK

\

Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


“Mario Charest” <> mcharest@clipzinformatic.com> > wrote in message
news:9u603h$fge$> 1@inn.qnx.com> …

“Ross Brantner” <> rbrantner01@charter.net> > wrote in message
news:9u5us6$en9$> 1@inn.qnx.com> …
I would like to compare 2 struct of the same type and determine if
they
are
equal, ie they have the same values in each data member. Is there a
way
I
can do this without comparing each data member? Sorry if the answer
is
obvious and I just don’t see it, I am a bit out of it today. Thanks.


The only portable way is to compare each data member one by one.
http://www.eskimo.com/~scs/C-faq/q2.8.html

A non portable way is to use memcmp(). I have never seen a Compiler/OS
where this didn’t work though.

Ross






\

Robert Krten, PARSE Software Devices +1 613 599 8316.
Realtime Systems Architecture, Consulting and Training at > www.parse.com
Email my initials at parse dot com.

nospam93@parse.com wrote:

Greg Comeau <> comeau@panix.com> > wrote:
In article <9u76e2$8rh$> 1@inn.qnx.com> >,
Bill Caroselli <> qtps@earthlink.net> > wrote:
I think that in a system where the data member alignment of a struct allows
padding between data members, a memcmp() could conceivably fail even though
a member by member compare would sucseed.

That’s right. The thing is that you can’t assume much about
the non-member space in a struct.

Ahh… gotcha. How about if I explicitly memset all the members and padding to
zero before each use – could I then guarantee that a memcmp works? I guess
my previous post puts egg on my face > :slight_smile:

The C standard doesn’t even guarantee that it works with simple ints –
pretty much any type other than unsigned char may contain padding bits
that don’t affect the integer value but do affect the result of
memcmp().

The C standard doesn’t even guarantee that you can safely set an int to
zero by memsetting all its bytes to zero – it may produce a trap
representation instead.

Of course, life is easier if you don’t have to make sure that your code
will run on all possible implementations of standard C, even the
wierdest ones. :slight_smile:


Wojtek Lerch QNX Software Systems Ltd.

Greg Comeau wrote:

In article <9u76e2$8rh$> 1@inn.qnx.com> >,
Bill Caroselli <> qtps@earthlink.net> > wrote:
I think that in a system where the data member alignment of a struct allows
padding between data members, a memcmp() could conceivably fail even though
a member by member compare would sucseed.

That’s right. The thing is that you can’t assume much about
the non-member space in a struct.

As each instance is constructed, you could memset it to zero (or whatever).



Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> > http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

In article <9u89bm$2jp$2@inn.qnx.com>, <nospam93@parse.com> wrote:

Greg Comeau <> comeau@panix.com> > wrote:
In article <9u76e2$8rh$> 1@inn.qnx.com> >,
Bill Caroselli <> qtps@earthlink.net> > wrote:
I think that in a system where the data member alignment of a struct allows
padding between data members, a memcmp() could conceivably fail even though
a member by member compare would sucseed.

That’s right. The thing is that you can’t assume much about
the non-member space in a struct.

Ahh… gotcha. How about if I explicitly memset all the members and padding to
zero before each use – could I then guarantee that a memcmp works?

Well, that assumes all bits zero makes sense for all members,
but otherwise, yeah.

I guess
my previous post puts egg on my face > :slight_smile:

You could say that :slight_smile:

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

<nospam93@parse.com> wrote in message news:9u89bm$2jp$2@inn.qnx.com

Greg Comeau <> comeau@panix.com> > wrote:
In article <9u76e2$8rh$> 1@inn.qnx.com> >,
Bill Caroselli <> qtps@earthlink.net> > wrote:
I think that in a system where the data member alignment of a struct
allows
padding between data members, a memcmp() could conceivably fail even
though
a member by member compare would sucseed.

That’s right. The thing is that you can’t assume much about
the non-member space in a struct.

Ahh… gotcha. How about if I explicitly memset all the members and
padding to
zero before each use – could I then guarantee that a memcmp works?

It’s the same general idea, memsetting a structure is non portable.
On x86 floating point will be equal to 0 after a memset, that is not
the case on non x86 processor.

Of course this is purely from a C language point of view. In practice most
people are doing that without any problem.

I personnaly use memset and memcmp without feeling any guilt :wink:
It’s a optimization trade off I’m willing to live with…



I guess my previous post puts egg on my face > :slight_smile:

-RK

Robert Krten, PARSE Software Devices +1 613 599 8316.
Realtime Systems Architecture, Consulting and Training at > www.parse.com
Email my initials at parse dot com.

“Greg Comeau” <comeau@panix.com> wrote in message
news:9u9bi9$qnt$1@panix3.panix.com

In article <9u89bm$2jp$> 2@inn.qnx.com> >, <> nospam93@parse.com> > wrote:
Greg Comeau <> comeau@panix.com> > wrote:
In article <9u76e2$8rh$> 1@inn.qnx.com> >,
Bill Caroselli <> qtps@earthlink.net> > wrote:
I think that in a system where the data member alignment of a struct
allows
padding between data members, a memcmp() could conceivably fail even
though
a member by member compare would sucseed.

That’s right. The thing is that you can’t assume much about
the non-member space in a struct.

Ahh… gotcha. How about if I explicitly memset all the members and
padding to
zero before each use – could I then guarantee that a memcmp works?

Well, that assumes all bits zero makes sense for all members,
but otherwise, yeah.

Just to throw more wood into the fire-- is it guaranteed not to have trash
written from cpu registers into the empty space when you update the struct?

Example- on intel cpu I have a char and then slack and I do
“mystruct.charmember = x;” the cpu takes x from maybe al register. Can it
maybe write all ax into “charmember plus slack” area? And then again you
dont know what is there anymore.

But p.s. I dont think so for any compiler I use.

In article <9ujg8p$hdb$1@inn.qnx.com>,
Captain High Lighter <capn@fish.net> wrote:

“Greg Comeau” <> comeau@panix.com> > wrote in message
news:9u9bi9$qnt$> 1@panix3.panix.com> …
In article <9u89bm$2jp$> 2@inn.qnx.com> >, <> nospam93@parse.com> > wrote:
Greg Comeau <> comeau@panix.com> > wrote:
In article <9u76e2$8rh$> 1@inn.qnx.com> >,
Bill Caroselli <> qtps@earthlink.net> > wrote:
I think that in a system where the data member alignment of a struct
allows padding between data members, a memcmp() could conceivably
fail even though a member by member compare would sucseed.

That’s right. The thing is that you can’t assume much about
the non-member space in a struct.

Ahh… gotcha. How about if I explicitly memset all the members and
padding to zero before each use – could I then guarantee that
a memcmp works?

Well, that assumes all bits zero makes sense for all members,
but otherwise, yeah.

Just to throw more wood into the fire-- is it guaranteed not to have
trash written from cpu registers into the empty space when you update
the struct?

Depends what you mean by “update”.

Example- on intel cpu I have a char and then slack and I do
“mystruct.charmember = x;” the cpu takes x from maybe al register. Can it
maybe write all ax into “charmember plus slack” area? And then again you
dont know what is there anymore.

But p.s. I dont think so for any compiler I use.

Assigning to .charmember wouldn’t write into the slack area,
though I think that technically it’s allow to (I haven’t looked
at this part of the standard in probably a decade so I could be wrong).

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?