编译问题 [英] complier problem

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

问题描述




我的问题是:

编译器是否禁止表达式* p ++ = c。

书中给出的答案是:不。因为这里即使p / v
的p被执行两次,它也用于修改两个不同的对象p和

* p。

所以任何人都可以向我详细解释这个答案

Hi

My question is : Would the expression *p++=c be disallowed by the
complier.
The answer given in the book is: No.Because here even though the vlue
of p is acced twice it is used to modify two different objects p and
*p.
so can anyone explain this answer to me detail

推荐答案




neha写于07/28/06 10:52,:


neha wrote On 07/28/06 10:52,:




我的问题是:表达式* p ++ = c被

编译器禁止。


书中给出的答案是:不。因为这里尽管vlue

的p被执行两次它用于修改两个不同的对象p和

* p。


所以任何人都可以解释这个答案给我详细信息
Hi

My question is : Would the expression *p++=c be disallowed by the
complier.
The answer given in the book is: No.Because here even though the vlue
of p is acced twice it is used to modify two different objects p and
*p.
so can anyone explain this answer to me detail



之前:


c = 42


p - [ 3]

[1]

[4]

后:


c = 42

[42]

p - [1 ]

[3]


有两件事发生了变化:c的值被复制了

到以前指向的对象p to,和p已经增加指向下一个对象的
。第一个是由*(某事)= c引起的
,第二个是由p ++引起的。


-
呃********* @ sun.com

Before:

c = 42

p --[ 3 ]
[ 1 ]
[ 4 ]

After:

c = 42

[ 42 ]
p --[ 1 ]
[ 3 ]

Two things have changed: The value of c has been copied
to the object p formerly pointed to, and p has been
incremented to point to the next object. The first was
caused by *(something) = c, the second by the p++.

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

neha写道:
neha wrote:

...

我的问题是:


编译器。

...

书中给出的答案是:不。因为这里虽然vlue
$ b p的$ b被执行两次它用于修改两个不同的对象p和

* p。

...

...
My question is : Would the expression *p++=c be disallowed by the
complier.
...
The answer given in the book is: No.Because here even though the vlue
of p is acced twice it is used to modify two different objects p and
*p.
...



该问题没有为答案提供足够的信息。给定的

答案根本没有意义。它是什么书?如果它说出你说的话

说,那么你最好得到另一本书。


注意,问题是关于表达式是否允许

或编译器不允许,换句话说,表达式是否为b $ b b格式良好或格式错误。为了回答这个问题,我们需要知道p和c的类型。例如,如果''p''的类型为''void *'',则将''++''运算符应用于它是非法的

,编译器将禁止它。如果''p''是
类型''double *''和''c''类型为''struct Foo'',那么我们就完全没有了
分配中不相关的类型,这也将导致来自

编译器的诊断。等等。没有这些额外的信息,就没有办法说b / b $ b $是否有必要让编译器抱怨的东西。


另一方面,建议的答案似乎解决了众所周知的违反对象访问和序列点规则的问题(不知道更好的名字)

for it)。即使在某些代码中违反了这个规则,它也不会使代码形成错误,即编译器不需要诊断。它不会是不允许b $ b不允许由编译器即使存在问题。相反,代码

将产生未定义的行为。还有很多其他的方法,顺便说一下,上面的表达式如何产生未定义的行为。但这是无关紧要的,因为它&b
与被编译器禁止无关。


-

祝你好运,

Andrey Tarasevich

The question does not provide enough information for an answer. And the given
answer does not make sense at all. What book is it? If it says what you say it
says, then you better get another book.

Note, that the question is specifically about whether the expression is allowed
or disallowed by the compiler or, in other words, whether the expression is
well-formed or ill-formed. In order to answer the question we need to know the
types of ''p'' and ''c''. For example, if ''p'' is of type ''void*'', then it is illegal
to apply the ''++'' operator to it and the compiler will disallow it. If ''p'' is of
type ''double*'' and ''c'' is of type ''struct Foo'', then we have completely
unrelated types in assignment, which will also cause a diagnostic from the
compiler. And so on. Without having this extra information, there''s no way to
say whether there''s something there that must cause the compiler to complain.

The proposed answer, on the other hand, seems to address the well-known issue of
violating the object-access-and-sequence-points rule (don''t know a better name
for it). Even if this rule is violated in some code, it does not make the code
ill-formed, i.e. no diagnostic is required from the compiler. It won''t be
"disallowed" by the compiler even if there''s a problem there. Instead, the code
will produce undefined behavior. There many are other ways, BTW, how the above
expression might produce undefined behavior. But this is irrelevant, since it
has nothing to do with being "disallowed by the compiler".

--
Best regards,
Andrey Tarasevich


neha发布:
neha posted:

我的问题是:

编译器是否禁止表达式* p ++ = c。
My question is : Would the expression *p++=c be disallowed by the
complier.



我认为这是一个错字:


* p ++ = p;

这意味着:


取p指向的对象,并存储值p。在里面。然后

增量p。很像:


* p = p;


++ p;


其中,在完整的上下文中,必须是这样的:


int main()

{

void * ptr [2] = 0;


void ** p = ptr;


* p ++ = p;

}


-


Frederick Gotham


I presume that that''s a typo for:

*p++ = p;
Which means:

Take the object pointed at by "p", and store the value "p" in it. Then
increment "p". Quite like:

*p = p;

++p;

Which, in full context, would have to be something like:

int main()
{
void *ptr[2] = 0;

void **p = ptr;

*p++ = p;
}

--

Frederick Gotham


这篇关于编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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