比较结构...... [英] Comparing structs...

查看:68
本文介绍了比较结构......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用" =="比较两个多字段变量(

相同结构)的内容是否合法和'!=" ;?


struct

{

int a;

int b;

} x,y;


...


if(x == y)/ /?

或者,无论出于何种原因,编译器都有可能使用两个变量的不同布局/填充,即使它们是

相同的结构?

解决方案

" barcaroller" < BA ********* @ music.net>写道:

使用" =="比较两个多字段变量(
相同结构)的内容是否合法?和'!=" ;?

结构
{
int a;
int b;
} x,y;

...

if(x == y)//?


否,==和!=运算符没有为结构定义。

(虽然分配是。)

或者编译器有可能因为某种原因使用不同的
两个变量的布局/填充,即使它们是相同的结构?




布局由类型决定,因此保证是

相同。具体来说,a和b的大小和偏移量以及整个结构的大小为x,与y相同。如果你给结构类型命名,这将是

a更清楚:


struct foo {

int a;

int b;

};


struct foo x,y;


你可以使用memcmp()来确定两个对象是否具有相同的

表示(这会检查所有位),但编译器是免费的

在成员之间或之后插入填充最后一个,填充的

内容可能是垃圾。


比较x和y的唯一真正方法是比较每个成员

明确:


xa == ya&& xb == yb


-

Keith Thompson(The_Other_Keith) ks * **@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


2006年3月29日星期三01:30:55 +0000,Keith Thompson写道:


不,==和!=运算符没有为结构定义。
(虽然分配。)




只是好奇:为什么? (是否定义了赋值但不是== an!=。)


如果你有能力将一个对象复制到另一个对象上,你应该认为还能比较它们吗?


问候,

马克。


Mark< ma **** @ tiscali.nl>在新闻中写道:pan.2006.03.29.05.43.47.997972

@ tiscali.nl:

2006年3月29日星期三01:30:55 + 0000,Keith Thompson写道:


不,==和!=运算符没有为结构定义。
(虽然分配。)



只是好奇:为什么? (是否定义了赋值但不是== an!=。)

它会认为如果你能够将一个对象复制到另一个对象上你还应该能够比较它们吗? / blockquote>


我不知道实际的理由是什么,但以下

参数没有说明结构应该如何比较是有道理的

我。


比较定义不明确。例如,如果struct具有指针

成员,则相等意味着指针的相等性,或者这些指针指向的值的相等性



或者,所有字段都与比较相关吗?


思南


-

A. Sinan Unur< 1u ** @ llenroc.ude.invalid>

(删除.invalid并反转每个组件的电子邮件地址)



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?

解决方案

"barcaroller" <ba*********@music.net> writes:

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) // ?
No, the "==" and "!=" operators are not defined for structs.
(Assignment is, though.)
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?



The layout is determined by the type, so that''s guaranteed to be the
same. Specifically, the size and offset of a and b, and the size of
the entire struct, is the same for x as it is for y. This would be
a bit clearer if you gave the struct type a name:

struct foo {
int a;
int b;
};

struct foo x, y;

You can use memcmp() to determine whether two objects have the same
representation (this examines all the bits), but the compiler is free
to insert padding between members or after the last one, and the
contents of the padding can be garbage.

The only real way to compare x and y is to compare each member
explicitly:

x.a == y.a && x.b == y.b

--
Keith Thompson (The_Other_Keith) 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.


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?

Regards,
Mark.


Mark <ma****@tiscali.nl> wrote in news:pan.2006.03.29.05.43.47.997972
@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?

Sinan

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)


这篇关于比较结构......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆