以下哪种组合的post&预增量运算符在C中有未定义的行为? [英] Which of the following combinations of post & pre-increment operators have undefined behaviour in C?

查看:163
本文介绍了以下哪种组合的post&预增量运算符在C中有未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读,任何人都可以解释这些未定义的行为(我= i ++ + ++ i,i = i ++,etc ...)尝试了解 comp.lang.c FAQ上的序列点,浪费了2个多小时的时间,试图通过gcc编译器解释以下结果。

I've read, Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...) and tried understanding Sequence points on "comp.lang.c FAQ" after wasting more than 2 hours of time trying to explain the following results by gcc compiler.

expression(i=1;j=2)     i       j       k
k = i++ + j++;          2       3       3
k = i++ + ++j;          2       3       4
k = ++i + j++;          2       3       4
k = ++i + ++j;          2       3       5

k = i++ + i++;          3               2
k = i++ + ++i;          3               4
k = ++i + i++;          3               4
k = ++i + ++i;          3               6

i = i++ + j++;          4       3
i = i++ + ++j;          5       3
i = ++i + j++;          4       3
i = ++i + ++j;          5       3

i = i++ + i++;          4
i = i++ + ++i;          5
i = ++i + i++;          5
i = ++i + ++i;          6

问题:


  1. 我想知道上图中显示的所有表达式(4组)是否具有未定义的行为?如果只有其中一些具有未定义的行为,哪些不行,哪些不行?

  1. I want to know if all the expressions shown (in 4 groups) in above figure have undefined behavior? If only some of them have undefined behavior which ones does and which ones doesn't?

对于定义的行为表达式,你可以很好地显示(不解释)编译器如何评估它们。只是为了确保,如果我得到这个预增量和

For defined behaviour expressions, kindly can you show (not explain) how compiler evaluates them. Just to make sure, if I got this pre-increment & post increment correctly.

背景:

今天,我参加了一次校园面试,我被要求解释给定值<$的结果 i ++ + ++ i C $ C>我。在gcc中编译该表达式后,我意识到我在面试中给出的答案是错误的。我决定今后不要犯这样的错误,因此,试图编译前后增量操作符的所有可能组合,并在gcc中编译,然后尝试解释结果。我挣扎了两个多小时。我找不到这些表达式的评估的单一行为。所以,我放弃了并转向了stackoverflow。在阅读档案后,发现有一些类似序列点和未定义的行为。

Today, I've attended a campus interview, in which I was asked to explain the results of i++ + ++i for a given value of i. After compiling that expression in gcc, I realized that the answer I gave in interview was wrong. I decided not to make such mistake in future and hence, tried to compile all possible combinations of pre and post increment operators and compile them in gcc and then try to explain the results. I struggled for more than 2 hours. I couldn't find single behaviour of evaluation of these expressions. So, I gave up and turned to stackoverflow. After little bit of reading archives, found that there is something like sequence point and undefined behaviour.

推荐答案

除了第一个组外,其他三个组中的所有表达式都有未定义的行为。

Except the first group, all expressions in the other three groups have undefined behaviour.

如何评估定义的行为(组1 ):

i=1, j=2;

k=i++ + j++; // 1 + 2 = 3
k=i++ + ++j; // 1 + 3 = 4
k=++i + ++j; // 2 + 3 = 5
k=++i + j++; // 2 + 2 = 4

这是相当简单的。 post-increment vs pre-increment thing。

It's fairly straight forward. post-increment vs pre-increment thing.

在组2和组4中,很容易看到未定义的行为。

In group 2 and group 4, it's quite easy to see the undefined behaviours.

组2具有未定义的行为,因为 = 运算符不会引入序列点。

Group 2 has undefined behaviour because = operator doesn't introduce a sequence point.

这篇关于以下哪种组合的post&amp;预增量运算符在C中有未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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