int[] arr={0}; int 值 = arr[arr[0]++]; 值 = 1?
今天,我看到了 Eric Lippert 撰写的一篇文章,他试图澄清运算符优先级之间的迷思以及评估的顺序。 最后有两个代码片段让我感到困惑,这是第一个片段:
int[] arr = {0};
int value = arr[arr[0]++];
现在,当我考虑变量 value 的值时,我只是将其计算为 1。 这就是我认为它是如何工作的。
- 首先声明 arr 为 int 数组 里面有一件物品; 这 item 的值为 0。
- 其次获取 arr[0] --0 中的值 这个案例。
- 第三步获取arr的值[值 步骤 2](仍然是 0)--gets arr[0]再次--仍然是0。
- 第四次分配步骤3的值 (0) 为变量值。 --值= 0 now
- 添加到步骤 2 的值 1 --Now arr[0] = 1。
显然这是错误的。 我尝试在 C# 规范中搜索一些有关增量何时实际发生的明确声明,但没有找到任何内容。
第二个片段来自 Eric 的评论 关于该主题的博客文章:
int[] data = { 11, 22, 33 };
int i = 1;
data[i++] = data[i] + 5;
现在我认为这个程序将如何执行——在声明数组并将 1 分配给 i 之后。 [请耐心听我说]
- 获取 data[i] --1
- 将步骤 1 的值添加到该值 5 --6
- 赋值给data[i](仍然是1) 步骤2的值 --data[i] = 6
- Increment i -- i = 2
根据我的理解,这个数组现在应该包含值{11, 27, 33}。 但是,当我循环打印数组值时,我得到:{11, 38, 33}。 这意味着 post 增量发生在取消引用数组之前!
怎么会? 这个岗位增量不应该是岗位吗? 即发生在其他一切之后。
伙计们,我错过了什么?
Today I came a cross an article by Eric Lippert where he was trying to clear the myth between the operators precedence and the order of evaluation. At the end there were two code snippets that got me confused, here is the first snippet:
int[] arr = {0};
int value = arr[arr[0]++];
Now when I think about the value of the variable value, I simply calculate it to be one. Here's how I thought it's working.
- First declare arr as an array of int
with one item inside of it; this
item's value is 0. - Second get the value of arr[0] --0 in
this case. - Third get the value of arr[the value
of step 2] (which is still 0) --gets
arr[0] again --still 0. - Fourth assign the value of step 3
(0) to the variable value. --value =
0 now - Add to the value of step 2 1 --Now
arr[0] = 1.
Apparently this is wrong. I tried to search the c# specs for some explicit statement about when the increment is actually happening, but didn't find any.
The second snippet is from a comment of Eric's blog post on the topic:
int[] data = { 11, 22, 33 };
int i = 1;
data[i++] = data[i] + 5;
Now here's how I think this program will execute --after declaring the array and assigning 1 to i. [plz bear with me]
- Get data[i] --1
- Add to the value of step 1 the value
5 --6 - Assign to data[i] (which is still 1)
the value of step 2 --data[i] = 6 - Increment i -- i = 2
According to my understanding, this array now should contain the values {11, 27, 33}. However, when I looped to print the array values I got: {11, 38, 33}. This means that the post increment happened before dereferencing the array!
How come? Isn't this post increment supposed to be post? i.e. happen after everything else.
What am I missing guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
后递增操作作为计算整个表达式的一部分发生。 这是在计算值之后但在计算任何其他表达式之前发生的副作用。
换句话说,对于任何表达式 E,E++(如果合法)表示类似(伪代码)的内容:
这是计算 E++ 的全部部分,然后再计算其他内容。
有关更多详细信息,请参阅 C# 3.0 规范的第 7.5.9 节。
此外,对于 LHS 被分类为变量的赋值操作(如本例所示),LHS 在评估 RHS 之前评估。
因此,在您的示例中:
相当于:
规范的相关位是第 7.16.1 节(C# 3.0 规范)。
The postincrement operation occurs as part of evaluating the overall expression. It's a side effect which occurs after the value is evaluated but before any other expressions are evaluated.
In other words, for any expression E, E++ (if legal) represents something like (pseudo-code):
That's all part of evaluating E++, before anything else is evaluated.
See section 7.5.9 of the C# 3.0 spec for more details.
Additionally, for assignment operations where the LHS is classified as a variable (as in this case), the LHS is evaluated before the RHS is evaluated.
So in your example:
is equivalent to:
The relevant bit of the specification for this is section 7.16.1 (C# 3.0 spec).
对于第一个片段,顺序是:
value
中。对于第二个片段,计算仍然是从左到右。
缺少的一点是“post”并不意味着“在其他所有事情之后”。 它只是意味着“在我检索该变量的当前值之后立即”。 在一行代码的“中间”发生后增量是完全正常的。
For the first snippet, the sequence is:
value
.For the second snippet, the evaluation is still left-to-right.
The missing piece is that "post" doesn't mean "after EVERYTHING else." It just means "immediately after I retrieve the current value of that variable." A post increment happening "in the middle of" a line of code is completely normal.
我希望后递增运算符在使用变量的值后递增变量。
在这种情况下,变量在第二次引用该变量之前递增。
如果不是这样,你可以写
如果它像你说的那样,那么你可以删除增量运算符,因为它实际上没有做任何事情,在我报告的指令中。
I would expect the post-increment operator to increment the variable after its value is used.
In this case, the variable is incremented before the second reference to the variable.
If it would not be so, you could write
If it would be like you say, then you could remove the increment operator because it doesn't do actually anything, in the instruction I reported.
您必须分三个步骤考虑赋值:
如果您有类似的内容
那么 A () 将首先运行,然后 C() 将运行,然后属性设置器 B 将运行。
本质上,你必须将你的陈述视为
You have to think of assignments in three steps:
If you have something like
Then A() will run first, then C() will run, and then the property setter B will run.
Essentially, you have to think of your statement as
原因可能是某些编译器将 i++ 优化为 ++i。 大多数时候,最终结果是相同的,但在我看来,这是编译器错误的罕见情况之一。
我现在无法访问 Visual Studio 来确认这一点,但请尝试禁用代码优化并查看结果是否保持不变。
The cause might be that some compilers optimize i++ to be ++i. Most of the time, the end result is the same, but it seems to me to be one of those rare occasions when the compiler is wrong.
I have no access to Visual Studio right now to confirm this, but try disabling code optimization and see if the results will stay the same.