473,882 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing structs...


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;

...

if (x==y) // ?
Or is there a chance that the compiler may, for whatever reason, use
different layouts/paddings for the two variables even though they are of the
same struct?
Mar 29 '06
12 26026
On 2006-03-29, Keith Thompson <ks***@mib.or g> wrote:
"A. Sinan Unur" <1u**@llenroc.u de.invalid> writes:
Mark <ma****@tiscali .nl> wrote in news:pan.2006.0 3.29.05.43.47.9 97972
@tiscali.nl:
On Wed, 29 Mar 2006 01:30:55 +0000, Keith Thompson wrote:
No, the "==" and "!=" operators are not defined for structs.
(Assignment is, though.)

Just curious: why? (Is assignment defined but not == an !=.)

It would think that if you are capable of copying one object to an other
you should also be capable of comparing them?


I don't claim to know what the actual rationale is but the following
argument for not specifiying how structs should be compared makes sense to
me.

Comparison is not well defined. For example, if the struct has pointer
members, does equality mean equality of pointers, or equality of values
pointed to by those pointers.

Or, are all fields relevant for the comparison?


I don't think it would make sense to follow pointer members, any more
than "==" on pointers should compare the objects they point to.

There are two plausible ways to define structure comparison. One
would be equivalent to memcmp(), but that would cause problems with
padding bytes. Defining it apply "==" to each member (and each
recursively to members of members) makes sense (<OT>Ada does
this</OT>), but it requires the compiler to generate considerable code
for (x == y), which is generally considered to be against the "spirit
of C".


A compiler could use memcmp and ensure that all padding bits/bytes are
always equal to zero.

It doesn't take considerable code to use "=" on a struct, or to pass or
return it? returning structs was once very messy.
Mar 29 '06 #11
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-03-29, Keith Thompson <ks***@mib.or g> wrote: [...]
There are two plausible ways to define structure comparison. One
would be equivalent to memcmp(), but that would cause problems with
padding bytes. Defining it apply "==" to each member (and each
recursively to members of members) makes sense (<OT>Ada does
this</OT>), but it requires the compiler to generate considerable code
for (x == y), which is generally considered to be against the "spirit
of C".


A compiler could use memcmp and ensure that all padding bits/bytes are
always equal to zero.


How could it ensure that all padding is always zero? Consider structs
allocated by malloc(). Consider structs read from a file (which
introduces portability problems anyway, but at least it works when the
same program writes and reads the same file).

Also consider members that are unions, floating-point members that
might be NaNs, and pointer members on an implementation where pointer
equality can't be done as a bitwise comparison.

Even if all this were worked out, the resulting "==" operator wouldn't
necessarily correspond to what the user wants to do. And how often do
you really want to compare structures for equality anyway?

All these things could have been resolved, but I agree with the
committee that it wasn't worthwhile.
It doesn't take considerable code to use "=" on a struct, or to pass or
return it? returning structs was once very messy.


It's not nearly as messy as "==" would be -- and considerably more
useful.

There's always a tradeoff in deciding whether to add a feature to the
language, and it can always be argued that the decision should have
been made differently.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #12
On Wed, 29 Mar 2006 21:19:40 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.or g> wrote:
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-03-29, Keith Thompson <ks***@mib.or g> wrote:[...]
A compiler could use memcmp and ensure that all padding bits/bytes are
always equal to zero.


How could it ensure that all padding is always zero? Consider structs
allocated by malloc().


There are several ways this could be done, but they'd impose a
significant performance penalty. For instance the compiler could
insist that all struct padding bits are set to zero when a struct is
created, or it could copy the struct before comparison, find and zero
any padding bits, then do the compare.
Also consider members that are unions, floating-point members that
might be NaNs, and pointer members on an implementation where pointer
equality can't be done as a bitwise comparison.


These are rather more significant issues, I suspect.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 29 '06 #13

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

Similar topics

6
8330
by: Alatarie | last post by:
Hi all, This is probably a stupid question but I've been out of the c++ thing for a while... I'm trying to make a vector of structs. Each time I create a struct: struct RECORDS { int id; other vars
8
2063
by: Martin Marcher | last post by:
Hi, I'm working on a program that creates a linear list of structs struct lin_list{ struct lin_list *next; char Name; }; this is what it looks like. And i got (design) problems with the search
18
30280
by: Rick | last post by:
Hi, We all know that Java has classes but how about basic storage objects like structs? C and C++ have Structs, Pascal has Records, Visual Basic has Types etc. How about Java? Greetings, Rick
5
2248
by: Bob Gregory | last post by:
Hi all, I'm utter C# newbie, do be gentle. VS2005 Express refuses point blank to install on my box, so I'm stuck with C# 1.0 unless someone can point me to a C#2.0 compiler elsewhere. I have an authentication system rigged up, which is working, but I'd like to be able to run a test like this <code>
5
2221
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where I'm comparing two structs. The struct test1 has information with regards to data_size and pointer to address. The struct test2 has information with regards to data_size and value. I will compare test1 and test2. For each matching data size,...
19
3860
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that handles GetHashCode and Equal? If so, is such a wrapper present in the standard class library?
61
3799
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
2
5498
by: Zytan | last post by:
I wanted to do something simple like checking my own Color variable against Color.Black, and == returned false, even though their RGB values were all 0! Of course, there's the A (alpha) value, but that's 0, too (I assume). I guess I naively thought that == would be overloaded to work in this case, just dealing with RGB. I guess I have to compare R, G, and B separately. Is this the standard way to deal with this? I know some structs...
50
20354
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
0
9932
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11109
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9558
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7957
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3226
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.