作业语义的模糊性? [英] Ambiguity in semantics of assignments?

查看:52
本文介绍了作业语义的模糊性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些代码可以给我不同的结果,取决于编译器上的



[包括省略]


typedef foo {

int A,B;

} FOO;


int main (){

int A;

FOO foo;


A = 57;

foo .A = 42;


A = foo.A + = A; / *关键陈述* /


printf(" A =%d; foo.A =%d \ n",A,foo.A);


返回0;

}


使用Cygwin gcc 3.3.3,A和foo.A.得到99 br />
使用不同的编译器,A和foo都得到84.A.


gcc给出的结果是我的预期。关键语句的语义是否有任何

歧义?


- Paul

Here''s some code that''s giving me differing results, depending
on the compiler.

[includes omitted]

typedef foo {
int A,B;
} FOO;

int main() {
int A;
FOO foo;

A = 57;
foo.A = 42;

A = foo.A += A; /* crucial statement */

printf("A = %d; foo.A = %d\n",A,foo.A);

return 0;
}

Using Cygwin gcc 3.3.3, I get 99 for both A and foo.A.
Using a different compiler, I get 84 for both A and foo.A.

The result given by gcc is what I expected. Is there any
ambiguity in the semantics of the crucial statement here?

-- Paul

推荐答案

文章< 9b ************************* @ posting.google.com> ,

Paul Steckler< st *** @ ccs.neu.edu>写道:
In article <9b*************************@posting.google.com> ,
Paul Steckler <st***@ccs.neu.edu> wrote:
这里有一些代码,它们给了我不同的结果,取决于编译器。

[包括省略]

typedef foo {
int A,B;
} FOO;

int main(){
int A;
FOO foo;

A = 57;
foo.A = 42;

A = foo.A + = A; / *关键陈述* /

printf(" A =%d; foo.A =%d \ n",A,foo.A);

返回0;
}

使用Cygwin gcc 3.3.3,A和foo都得到99.A /
使用不同的编译器,A和foo都得到84 。一个。


(哪个编译器?)


gcc给出的结果是我的预期。这里关键语句的语义是否存在歧义?
Here''s some code that''s giving me differing results, depending
on the compiler.

[includes omitted]

typedef foo {
int A,B;
} FOO;

int main() {
int A;
FOO foo;

A = 57;
foo.A = 42;

A = foo.A += A; /* crucial statement */

printf("A = %d; foo.A = %d\n",A,foo.A);

return 0;
}

Using Cygwin gcc 3.3.3, I get 99 for both A and foo.A.
Using a different compiler, I get 84 for both A and foo.A.
(Which compiler?)

The result given by gcc is what I expected. Is there any
ambiguity in the semantics of the crucial statement here?




我什么也看不见。


第一个嫌疑人是调用未定义的行为,既改变对象'的价值又使用其值除了

以外的其他东西计算新值存储在序列点之间(N869

6.5#2),但你不是在这里做的。


你发布的代码加上#including < stdio.h中>对于printf,

a完整的程序来演示这个问题?

如果没有,那么你可能会切出一些东西,那就是

引入歧义;找到并修复它。如果是,特别是如果你通过直接使用两个int变量来获得不同的行为而不是将它们包装在一个结构中的
,那么你可能遇到了编译器错误。 />
dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca

比喻:你问的是一群小提琴家关于更好的点数

打大号。 ...... [L]让我们只有可怜的小提琴手;我们已经敲了很多

已经足够了。 --Eric Sosman in comp.lang.c



I don''t see any.

The first suspect is invoking undefined behavior by both changing
an object''s value and using an its value for something other than
calculating the new value to be stored between sequence points (N869
6.5#2), but you''re not doing that here.

Is the code you posted, plus #including <stdio.h> for the printf,
a complete program that demonstrates the problem?
If not, there''s probably something in the part you cut out that''s
introducing an ambiguity; find it and fix it. If it is, especially if
you get different behavior by using two int variables directly instead
of wrapping them in a struct, you may have encountered a compiler bug.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Analogy: you are asking a bunch of violinists about the finer points of
playing the tuba. ... [L]eave us poor violinists alone; we''re strung
out enough already. --Eric Sosman in comp.lang.c


Paul Steckler写道:
Paul Steckler wrote:
这里有一些代码让我有所不同结果,取决于编译器。

[包括省略]

typedef foo {
int A,B;
} FOO;

int main(){
int A;
FOO foo;

A = 57;
foo.A = 42; < br =>
A = foo.A + = A; / *关键陈述* /

printf(" A =%d; foo.A =%d \ n",A,foo.A);

返回0;
}

使用Cygwin gcc 3.3.3,A和foo都得到99.A /
使用不同的编译器,A和foo都得到84 .A。

gcc给出的结果是我的预期。这里关键陈述的语义是否有任何歧义?
Here''s some code that''s giving me differing results, depending
on the compiler.

[includes omitted]

typedef foo {
int A,B;
} FOO;

int main() {
int A;
FOO foo;

A = 57;
foo.A = 42;

A = foo.A += A; /* crucial statement */

printf("A = %d; foo.A = %d\n",A,foo.A);

return 0;
}

Using Cygwin gcc 3.3.3, I get 99 for both A and foo.A.
Using a different compiler, I get 84 for both A and foo.A.

The result given by gcc is what I expected. Is there any
ambiguity in the semantics of the crucial statement here?




这句话比含糊不清更糟糕;它没有意义

因为它存在于非C源中。一旦编译器

发出所需的诊断,就可以免费继续使用

翻译非C程序,但标准不再是

管理当程序执行时会发生什么。


如果有一个

`struct''或'union,情况会有所不同''关键字在'typedef'之后',或者

如果`foo''已经#define''d为'struct''或'union''本身。

如果是这样的话,就不会有歧义,

标准将保持有效,两个产出

将是99.

但是,正如代码所示,两个编译器都是正确的。 -

,当然他们已经发出了诊断信息。


(你*做了*剪切并粘贴实际代码,没有你呢?

当然你不会发布一个不准确的释义

不准确的释义然后要求诊断

罚款点语言法,你会吗?不,我确定你

不会做任何事情*那个*愚蠢 - 但话说再说一遍,

它'用语法

错误来折磨你的编译器是非常愚蠢的,然后尝试理解结果......)


-
Er*********@sun.com



The statement is worse than ambiguous; it''s meaningless
because it resides in a non-C source. Once the compiler
issues the required diagnostic it''s free to go ahead and
translate the non-C program, but the Standard no longer
governs what happens when and if the program is executed.

Things would have been different if there had been a
`struct'' or `union'' keyword right after the `typedef'', or
if `foo'' had been #define''d as `struct'' or `union'' itself.
Had that been the case, there would have been no ambiguity,
the Standard would have remained in force, and both outputs
would have been 99.

But as the code stands, both compilers are "right" --
provided they''ve issued that diagnostic, of course.

(You *did* cut and paste the actual code, didn''t you?
Surely you wouldn''t have been foolhardy enough to post an
inaccurate paraphrase and then ask for diagnosis of a
fine point of language law, would you? No, I''m sure you
wouldn''t have done anything *that* silly -- but then again,
it''s pretty silly to torment your compilers with syntax
errors and then try to make sense of the results ...)

--
Er*********@sun.com


Paul Steckler写道:
Paul Steckler wrote:
这里有一些代码可以给我不同的结果,
取决于编译器。
cat foo.c
#include< stdio.h>


typedef struct Foo {

int A,B;

} Foo;


int main(){

int A;

Foo foo;


A = 57;

foo.A = 42;


A = foo.A + = A; //关键陈述


printf(" A =%d \ tfoo.A =%d \ n",A,foo.A);


返回0;

}

gcc -Wall -std = c99 -pedantic -o foo foo.c
./foo
A = 99 foo.A = 99

使用Cygwin gcc 3.3.3,A和foo都得到99.A.
使用不同的编译器,我得到84 A和foo.A.

gcc给出的结果是我的预期。这里关键语句的语义是否存在歧义?
Here''s some code that''s giving me differing results,
depending on the compiler. cat foo.c #include <stdio.h>

typedef struct Foo {
int A, B;
} Foo;

int main() {
int A;
Foo foo;

A = 57;
foo.A = 42;

A = foo.A += A; // crucial statement

printf("A = %d\tfoo.A = %d\n", A, foo.A);

return 0;
}
gcc -Wall -std=c99 -pedantic -o foo foo.c
./foo A = 99 foo.A = 99
Using Cygwin gcc 3.3.3, I get 99 for both A and foo.A.
Using a different compiler, I get 84 for both A and foo.A.

The result given by gcc is what I expected. Is there any
ambiguity in the semantics of the crucial statement here?




Nope。



Nope.


这篇关于作业语义的模糊性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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