是+ ++相当于(n = n + 1)? [英] is ++n equivalent to (n=n+1) ?

查看:80
本文介绍了是+ ++相当于(n = n + 1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我已经养成了将精神表达转化为

解析tress的习惯(在阅读了一些clc帖子后开始这样做)解析

树)。


有人可以验证以下假设是否正确吗?


1) ++ n,我们可以(n = n + 1)因为它是等价的:


| =

| / \

| n +

| / \

| n 1

2)对于形式为n op = b的赋值运算符,等价树

为:


| =

| / \

| n op

| / \

| n b

3)对于n ++,可以使用n,但是条件是(n = n + 1)

出现在现在和最早的序列点在树上

(但是n = n + 1并不是树的实际部分)


|

| n

|

| [旁注:(n = n + 1)可以在

之前的任何时候出现副作用。最早的序列点在树上]


我的假设是否正确?

Hi,

I''ve gotten into the habit of mentally converting big expressions to
parse tress (started doing this after reading some c.l.c posts on parse
trees).

Can someone verify if the following assumptions are correct?

1) For ++n, one can us (n=n+1) as it''s equivalent:

| =
| / \
| n +
| / \
| n 1
2) For assignment operators of the form n op= b, the equivalent tree
is:

| =
| / \
| n op
| / \
| n b

3) For n++, one can use n, but with a side condition that (n=n+1) will
occur somewhere between now and the earliest sequence point up the tree
(but n=n+1 is no physically part of the tree)

|
| n
|
| [Side note: (n=n+1) can occure as a side effect anytime before the
| earliest sequence point up the tree]

Are my assumption correct?

推荐答案

In文章< 11 ********************** @ g14g2000cwa.googlegroups .com>,

Kobu< ko **** ****@gmail.com>写道:

:我已养成了将大表达式转化为精神的习惯

:解析tress


:可以有人验证以下假设是否正确?


:1)对于++ n,我们可以(n = n + 1)因为它是等价的:


编号假设n是一个表达式,它返回一个左值并且

有副作用。在n = n + 1中,n将分配两个

两侧的分配,导致副作用

两次。在n ++或n中,op = pn只会被评估一次。


例如,queuehead() - > count = queuehead() - > count + 1

与queuehead()不同 - > count ++


-

当你的帖子单独/和用户在电话上/

有一个地方需要检查 - /上游!

当你匆忙/传播是一个担心/

有一个地方你可以发布 - /上游!
In article <11**********************@g14g2000cwa.googlegroups .com>,
Kobu <ko********@gmail.com> wrote:
:I''ve gotten into the habit of mentally converting big expressions to
:parse tress

:Can someone verify if the following assumptions are correct?

:1) For ++n, one can us (n=n+1) as it''s equivalent:

No. Suppose n is an expression that returns an lvalue and which
has side effects. In n = n + 1 then n will be evaluated for both
sides of the assignment, resulting in the side effect being done
twice. In n++ or n op= p n will only be evaluated once.

For example, queuehead()->count = queuehead()->count + 1
differs from queuehead()->count++

--
When your posts are all alone / and a user''s on the phone/
there''s one place to check -- / Upstream!
When you''re in a hurry / and propagation is a worry/
there''s a place you can post -- / Upstream!


在文章< 11 ************** ********@g14g2000cwa.googlegroups .com> ;,

" Kobu" < KO ******** @ gmail.com>写道:
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Kobu" <ko********@gmail.com> wrote:


我已经养成了精神上将大表达式转换为解析tress的习惯(在阅读后开始这样做)解析
树上的一些clc帖子。

有人可以验证以下假设是否正确吗?

1)对于++ n,我们可以(n = n + 1)因为它是等价的:

| =
| / \
| n +
| / \
| n 1

2)对于形式为n op = b的赋值运算符,等效树
是:

| =
| / \
| n op
| / \
| n b


正确,除了左值n仅评估一次。所以


++(* ++ p); ++ a [i ++];


绝对不一样


(* ++ p)=(* ++ p) + 1; a [i ++] = a [i ++] + 1;

但对于像n这样的普通变量,它是正确的。 (2)非常重要如果

你需要弄清楚究竟会发生什么,例如你有一个

复杂的情况如


unsigned short us;

double d;


us + = d;


在类似的情况下那么,你需要做的就是阅读究竟是什么(我们+ d)

做了什么,然后你阅读了规则是什么将双重分配给无符号

确实。以这种方式定义它必须已经在C标准中保存了几十页



3)对于n ++,可以使用n,但条件是(n = n + 1)将发生在现在和最早的序列点之间的某个地方
(但是n = n + 1并不是树的实际部分)

|
| n
|
| [旁注:(n = n + 1)可以在
|之前的任何时候作为副作用发生最早的序列指向树]
Hi,

I''ve gotten into the habit of mentally converting big expressions to
parse tress (started doing this after reading some c.l.c posts on parse
trees).

Can someone verify if the following assumptions are correct?

1) For ++n, one can us (n=n+1) as it''s equivalent:

| =
| / \
| n +
| / \
| n 1
2) For assignment operators of the form n op= b, the equivalent tree
is:

| =
| / \
| n op
| / \
| n b
Correct, except that the lvalue n is only evaluated once. So

++(*++p); ++a [i++];

is absolutely not the same as

(*++p) = (*++p) + 1; a [i++] = a [i++] + 1;
but for a plain variable like n it is correct. (2) is quite important if
you need to figure out exactly what happens for example if you have a
complicated case like

unsigned short us;
double d;

us += d;

In a case like that, all you need to do is to read what exactly (us + d)
does, and then you read the rules what assigning a double to an unsigned
short exactly does. Defining it this way must have saved dozens of pages
in the C Standard.
3) For n++, one can use n, but with a side condition that (n=n+1) will
occur somewhere between now and the earliest sequence point up the tree
(but n=n+1 is no physically part of the tree)

|
| n
|
| [Side note: (n=n+1) can occure as a side effect anytime before the
| earliest sequence point up the tree]




是的。



Yes.


Christian Bau写道:
Christian Bau wrote:
在文章< 11 ********************** @ g14g2000cwa.googlegroups .com>,
" Kobu" < KO ******** @ gmail.com>写道:
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Kobu" <ko********@gmail.com> wrote:

我已经养成了精神上转换大表达式的习惯
来解析tress(在阅读后开始这样做)
解析树上的一些clc帖子。

有人可以验证以下假设是否正确吗?

1)对于++ n,我们可以(n = n + 1)因为它是等价的:

| =
| / \
| n +
| / \
| n 1

2)对于n op = b形式的赋值运算符,等价的
树是:

| =
| / \
| n op
| / \
| n b
正确,除了左值n仅被评估一次。所以

++(* ++ p); ++ a [i ++];

绝对不一样

(* ++ p)=(* ++ p)+ 1; a [i ++] = a [i ++] + 1;

但对于像n这样的普通变量,它是正确的。 (2)非常重要
Hi,

I''ve gotten into the habit of mentally converting big expressions to parse tress (started doing this after reading some c.l.c posts on parse trees).

Can someone verify if the following assumptions are correct?

1) For ++n, one can us (n=n+1) as it''s equivalent:

| =
| / \
| n +
| / \
| n 1
2) For assignment operators of the form n op= b, the equivalent tree is:

| =
| / \
| n op
| / \
| n b
Correct, except that the lvalue n is only evaluated once. So

++(*++p); ++a [i++];

is absolutely not the same as

(*++p) = (*++p) + 1; a [i++] = a [i++] + 1;
but for a plain variable like n it is correct. (2) is quite important



如果你需要弄清楚究竟会发生什么,例如你有一个
复杂的情况如

unsigned short我们;
双d;

我们+ = d;

在这种情况下,您需要做的就是阅读究竟是什么(我们+
d)确实如此,然后你阅读规则是什么,为
unsigned short分配一个double。以这种方式定义它必须在C标准中保存了几十个
页面。


if you need to figure out exactly what happens for example if you have a complicated case like

unsigned short us;
double d;

us += d;

In a case like that, all you need to do is to read what exactly (us + d) does, and then you read the rules what assigning a double to an unsigned short exactly does. Defining it this way must have saved dozens of pages in the C Standard.

3)对于n ++,可以使用n,但是有一个边条件(n = n + 1)
将发生在现在和最早的序列点之间的某个地方
树(但是n = n + 1并不是树的实际部分)

|
| n
|
| [旁注:(n = n + 1)可以在
之前的任何时间发生副作用最早的序列指向树]
3) For n++, one can use n, but with a side condition that (n=n+1) will occur somewhere between now and the earliest sequence point up the tree (but n=n+1 is no physically part of the tree)

|
| n
|
| [Side note: (n=n+1) can occure as a side effect anytime before the | earliest sequence point up the tree]



是的。



Yes.



好​​点,我想我可以代表(exp)++其中exp有意义as

左值为:

| =

| / \

| temp +

| / \

| = 1

| / \

| temp exp< - exp真的会更加分支

| (执行一次)

其中(exp)=(exp)+ 1将是:


| =

| / \

| exp +< -

| / \ | - exp相关分支在两个地方

| exp 1< - (执行两次)

|


Good point, I guess I can represent (exp)++ where exp has meaning as
an lvalue as:
| =
| / \
| temp +
| / \
| = 1
| / \
| temp exp <-- exp would really be more branching
| (executed once)
where (exp) = (exp) + 1 would be:

| =
| / \
| exp + <--
| / \ |-- exp related branching in two places
| exp 1 <-- (executed twice)
|


这篇关于是+ ++相当于(n = n + 1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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