* p ++ = * q ++ undefined?为什么? [英] *p++ = *q++ undefined? why?

查看:103
本文介绍了* p ++ = * q ++ undefined?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我试图了解确定

表达式何时定义良好的规则是什么。我找到了一个页面

http:// c -faq.com/expr/seqpoints.html )这似乎很好地解释了

够了。


首先是一些条款:

* object = variable,数组元素,指针指向的位置

* modification = assignment(= + = ...),递增/递减

(后缀/前缀)


如果我理解正确,有两个规则(按顺序检查):

1)每个对象在一个表达式可以修改不超过一次;

2)如果一个对象被修改,那么该对象被访问的所有时间

为了被读取,值读取的必须全部用于

计算对象的最终值。


现在看一下 http://c-faq.com/expr/confused.html

(结束第3点):


* p ++ = * q ++


i无法理解以下句子:

[...]其中三个内容被修改(p,q和* p [...]),如果所有三个对象都是不同的,则允许
,即只有使用两个不同的指针p和q [...]时才使用。


i解释你我的推理。首先,表达式可以是

改写为:


((*(p ++))=(*(q ++)))


有4个对象:p,q,*(p ++),*(q ++)。


p,q和*(p ++)只修改一次,因此他们遵循规则1.

*(q ++)未修改,因此它也遵循规则1.


p只读一次,且值为这个读数用于计算p的最终值

,因此遵守规则2。

q一样。 *(p ++)永远不会读,所以它也尊重规则2.


那么,为什么表达式未定义?


说实话,它并没有说它是未定义的,但它说的是

是否被允许。所以:在这种情况下这个表达式是不允许的?

i什么时候看不到...


也,编译器(gcc)不抱怨如:


i = i ++ / *警告:i上的操作可能未定义* /


所以,这是什么问题?

hello,

i''m trying to understand what are the rules for determining when an
expression is well defined or not. i found a page
(http://c-faq.com/expr/seqpoints.html) which seems to explain this well
enough.

first of all some terms:
* object = variable, element of an array, location pointed by a pointer
* modification = assignment ( = += ... ), increment/decrement
(postfixed/prefixed)

if i understood correctly, there are two rules (checked in that order):
1) each object in an expression can be modified no more than once;
2) if an object is modified, then all the times that object is accessed
in order to be read, the values that are read must be all used for
calculating the final value of the object.

now give a look at the expression at http://c-faq.com/expr/confused.html
(end of point 3):

*p++ = *q++

i can''t understand the following sentence:

"[...] in which three things are modified (p, q, and *p [...]), are
allowed if all three objects are distinct, i.e. only if two different
pointers p and q [...] are used."

i explain you my reasoning. first of all that expression could be
rewritten as:

((*(p++)) = (*(q++)))

there are 4 objects: p, q, *(p++), *(q++).

p, q and *(p++) are modified only once, so they follow rule 1.
*(q++) is not modified, so it also follows rule 1.

p is read only once, and the value of this reading is used for
calculating the final value of p, so rule 2 is respected. the same for
q. *(p++) is never read, so it also respects rule 2.

so, why that expression is undefined?

to be honest, it does not say it is "undefined", but it speaks about
being allowed or not. so: in which case this expression is not allowed?
i can''t see when...

also, the compiler (gcc) does not complain as in:

i = i++ /* warning: operation on `i'' may be undefined */

so, what''s the problem?

推荐答案

fctk< - > opined:
fctk <-> opined:
你好,

我正在试图了解确定何时定义
表达式的规则是什么。我找到了一个页面
http://c-faq.com /expr/seqpoints.html )这似乎很好地解释了这个。

首先是一些术语:
* object = variable,数组的元素,位置指向
指针*修改=赋值(= + = ...),增量/减量
(后缀/前缀)

如果我理解正确,有两个规则(按照
顺序检查):1)表达式中的每个对象都可以修改一次不超过
; 2)如果一个对象被修改,那么所有对象都被访问以便被读取,所读取的值必须全部用于计算对象的最终值。现在看看
上的表达式http://c-faq.com/expr/confused.html (第3点结束):

* p ++ = * q ++

我可以'理解以下句子:

[...]允许修改三个内容(p,q和* p [...]),如果允许的话所有三个对象都是截然不同的,即只有两个不同的
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ $>
^^^^^^^^


这是理解这一点的关键。如果`p`和`q`指向内存中的不同对象,则赋值定义明确

* *如果他们

指向同一个对象就会得到未定义的行为。


这可能就是编译器没有发出诊断的原因,因为

有上面情况可以的情况。


无论如何,不​​要这样做。

指针p和q [...]是用过。

我向你解释我的理由。首先,表达式可以改写为:

((*(p ++))=(*(q ++)))

有4个对象: p,q,*(p ++),*(q ++)。

p,q和*(p ++)只修改一次,因此它们遵循规则1.
*(q ++)is没有修改,所以它也遵循规则1.

p只读一次,并且该读数的值用于计算p的最终值,因此遵守规则2。对于q来说一样。 *(p ++)永远不会读,所以它也尊重规则2.

那么,为什么那个表达式是未定义的?

说实话,它并不是说它是 ; undefined,但它说的是允许与否。所以:在这种情况下,这个表达式是不允许的?我什么时候都看不到...

此外,编译器(gcc)也没有抱怨如下:

i = i ++ / *警告:对`i的操作''可能未定义* /

所以,问题是什么?
hello,

i''m trying to understand what are the rules for determining when an
expression is well defined or not. i found a page
(http://c-faq.com/expr/seqpoints.html) which seems to explain this
well enough.

first of all some terms:
* object = variable, element of an array, location pointed by a
pointer * modification = assignment ( = += ... ), increment/decrement
(postfixed/prefixed)

if i understood correctly, there are two rules (checked in that
order): 1) each object in an expression can be modified no more than
once; 2) if an object is modified, then all the times that object is
accessed in order to be read, the values that are read must be all
used for calculating the final value of the object.

now give a look at the expression at
http://c-faq.com/expr/confused.html (end of point 3):

*p++ = *q++

i can''t understand the following sentence:

"[...] in which three things are modified (p, q, and *p [...]), are
allowed if all three objects are distinct, i.e. only if two different ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^

Here''s the key to understanding this. The assignment is well defined
*only* if `p` and `q` point to different objects in memory. If they
point to the same object you get undefined behaviour.

That is probably the reason for compiler not emitting a diagnostic, as
there are circumstances when above is OK.

Anyway, don''t do that.
pointers p and q [...] are used."

i explain you my reasoning. first of all that expression could be
rewritten as:

((*(p++)) = (*(q++)))

there are 4 objects: p, q, *(p++), *(q++).

p, q and *(p++) are modified only once, so they follow rule 1.
*(q++) is not modified, so it also follows rule 1.

p is read only once, and the value of this reading is used for
calculating the final value of p, so rule 2 is respected. the same
for q. *(p++) is never read, so it also respects rule 2.

so, why that expression is undefined?

to be honest, it does not say it is "undefined", but it speaks about
being allowed or not. so: in which case this expression is not
allowed? i can''t see when...

also, the compiler (gcc) does not complain as in:

i = i++ /* warning: operation on `i'' may be undefined */

so, what''s the problem?




-

简而言之,我的神学是宇宙被指示,但没有签署。

- 克里斯托弗莫利


< http ://clc-wiki.net/wiki/Introduction_to_comp.lang.c>



--
My theology, briefly, is that the universe was dictated but not
signed.
-- Christopher Morley

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


在2006-04-01,fctk< - >写道:
On 2006-04-01, fctk <-> wrote:
你好,我想知道什么是确定
表达式是否定义良好的规则。我找到了一个页面
http://c-faq.com /expr/seqpoints.html )似乎很好地解释了这个。
现在看一下 http://c-faq.com/expr/confused.html
(第3点结束) :

* p ++ = * q ++

我无法理解以下句子:

[...]其中三个事物被修改(p,q和* p [...]),如果所有三个对象都是不同的,则允许,即只有两个不同的指针p和q [...]是用过。

i解释你的推理。首先,表达式可以改写为:

((*(p ++))=(*(q ++)))

有4个对象: p,q,*(p ++),*(q ++)。


这根本不是同一个表达式。

i = i ++ / *警告:i上的操作可能未定义* /

所以,问题是什么?
hello,

i''m trying to understand what are the rules for determining when an
expression is well defined or not. i found a page
(http://c-faq.com/expr/seqpoints.html) which seems to explain this well
enough.

now give a look at the expression at http://c-faq.com/expr/confused.html
(end of point 3):

*p++ = *q++

i can''t understand the following sentence:

"[...] in which three things are modified (p, q, and *p [...]), are
allowed if all three objects are distinct, i.e. only if two different
pointers p and q [...] are used."
i explain you my reasoning. first of all that expression could be
rewritten as:

((*(p++)) = (*(q++)))

there are 4 objects: p, q, *(p++), *(q++).
That''s not the same expression at all.

i = i++ /* warning: operation on `i'' may be undefined */

so, what''s the problem?




这与FAQ中的表达方式完全不同。


我不确定为什么你会编写这样的代码,但我看不到

表达会产生那些问题(IANALL)



That''s not the same expression as in the FAQ at all.

I''m not certain why you''d ever write code like that, but I can''t see
there that expression would create those sorts of problems (IANALL)


fctk schrieb:
fctk schrieb:
你好,

我想了解确定
表达式何时定义良好的规则是什么。我找到了一个页面
http://c-faq.com /expr/seqpoints.html )似乎很好地解释了这个。

首先是一些术语:
* object = variable,数组的元素,指针指向的位置
*修改=赋值(= + = ...),增量/减量
(后缀/前缀)

如果我理解正确,有两个规则(按顺序检查):
1)表达式中的每个对象可以修改不超过一次;
2)如果一个对象被修改,那么该对象被访问的所有时间
为了读取,读取的值必须全部用于计算对象的最终值。
现在看一下 http://c-faq.com/expr/confused.html
(第3点结束):

* p ++ = * q ++

我不能理解以下句子:

[...]其中三个东西被修改(p,q和* p [...]),如果全部三个都被允许对象是不同的,即仅使用两个不同的指针p和q [...]。


有两种可能的解释:

- p == q是不允许的,

- & p ==& ; q是不允许的,例如我们一定不要写

* p ++ = * p ++


对于以下我假设p!= 0,q!= 0.

((*(p ++))=(*(q ++)))

有4个对象: p,q,*(p ++),*(q ++)。


让我们更容易保持这个:

p,q,* p,* q

p,q和*(p ++)仅修改一次,因此它们遵循规则1.
*(q ++)未修改,因此它也遵循规则1.


如果p!= q,即p并且q指向不同的对象,那么你就可以实现。以上通过

1)将* q复制到tmp

2)将q增加一个

3)将p复制到ptmp

4)将p增加一个

5)将tmp复制到* ptmp并返回 tmp

我们可以重新订购1-3-2-4-5,1-3-4-2-5,3-4-1-2-5,3-1-4- 2-5,

3-1-2-4-5不破坏它。

如果我们假设p == q,我们就不能打破这个。

仅当我们更换q时通过p,那么一切都不是以1)

和3)开头的前两个位置休息。

p只读一次,其值为读数用于计算p的最终值,因此遵守规则2。对于
q。 *(p ++)永远不会读,所以它也尊重规则2.

那么,为什么那个表达式是未定义的?


我想第二种解释意味着。

阅读整个项目3.从页面中暗示差异

实际上并不意味着p!= q,但在某种意义上& p!=& q。

说实话,它并没有说它是未定义的,但是它说的是允许与否。所以:在这种情况下这个表达式是不允许的?
我什么时候都看不到......

此外,编译器(gcc)也没有抱怨如下:

i = i ++ / *警告:i上的操作可能未定义* /

所以,问题是什么?
hello,

i''m trying to understand what are the rules for determining when an
expression is well defined or not. i found a page
(http://c-faq.com/expr/seqpoints.html) which seems to explain this well
enough.

first of all some terms:
* object = variable, element of an array, location pointed by a pointer
* modification = assignment ( = += ... ), increment/decrement
(postfixed/prefixed)

if i understood correctly, there are two rules (checked in that order):
1) each object in an expression can be modified no more than once;
2) if an object is modified, then all the times that object is accessed
in order to be read, the values that are read must be all used for
calculating the final value of the object.

now give a look at the expression at http://c-faq.com/expr/confused.html
(end of point 3):

*p++ = *q++

i can''t understand the following sentence:

"[...] in which three things are modified (p, q, and *p [...]), are
allowed if all three objects are distinct, i.e. only if two different
pointers p and q [...] are used."
There are two possible interpretations:
- p == q is not allowed,
- &p == &q is not allowed, e.g. we must not write
*p++ = *p++

For the following I assume p != 0, q != 0.

i explain you my reasoning. first of all that expression could be
rewritten as:

((*(p++)) = (*(q++)))

there are 4 objects: p, q, *(p++), *(q++).
Let us keep this even easier:
p, q, *p, *q
p, q and *(p++) are modified only once, so they follow rule 1.
*(q++) is not modified, so it also follows rule 1.
If p!=q, i.e. p and q point to different objects, then you
could "implement" the above by
1) copying *q to tmp
2) increasing q by one
3) copying p to ptmp
4) increasing p by one
5) copying tmp to *ptmp and "returning" tmp
which we can reorder to 1-3-2-4-5, 1-3-4-2-5, 3-4-1-2-5, 3-1-4-2-5,
3-1-2-4-5 without breaking it.
If we assume p==q, we cannot break this.
Only if we replace "q" by "p", then everything not starting with 1)
and 3) in the first two positions breaks.

p is read only once, and the value of this reading is used for
calculating the final value of p, so rule 2 is respected. the same for
q. *(p++) is never read, so it also respects rule 2.

so, why that expression is undefined?
I guess that the second interpretation is meant.
Reading the whole item 3. from the page implies that "difference"
really is not meant in the sense p != q but in the sense &p != &q.
to be honest, it does not say it is "undefined", but it speaks about
being allowed or not. so: in which case this expression is not allowed?
i can''t see when...

also, the compiler (gcc) does not complain as in:

i = i++ /* warning: operation on `i'' may be undefined */

so, what''s the problem?



HTH

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



HTH
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于* p ++ = * q ++ undefined?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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