是否有“顺序点"?像"int a = 4,* ptr =& a;"这样的语句会出现问题或"x + = 4,y = x * 2;"? [英] Are there "sequence-point" issues with statements like "int a=4,*ptr=&a;" or "x+=4,y=x*2;"?

查看:79
本文介绍了是否有“顺序点"?像"int a = 4,* ptr =& a;"这样的语句会出现问题或"x + = 4,y = x * 2;"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对整个序列点事物的理解是基本的.我所拥有的只是一个粗略的直观想法,即一旦遇到序列点,我们就可以确保先前评估的所有副作用都是完整的".我还读到,在类似printf("%d",a++,++a,a++)的语句中,行为未定义,因为逗号不表示序列点,而分号则表示序列点.因此,我觉得这是一个非常严格和结论性的答案,而不是凭直觉进行猜测,将对我有很大帮助.

My understanding of the whole sequence points thing is basic. All I have is some crude intuitive idea that "once a sequence point is encountered, we can be sure all side effects of previous evaluations are complete". I also read that in statements like printf("%d",a++,++a,a++) the behavior is undefined as a comma doesn't signify a sequence point while a semi-colon does. So instead of making guesses and going by intuition, I feel a very rigorous and conclusive answer to this will help me a lot.

以下类型的语句也是安全的& ;;肯定在C中:

So are the following kind of statements safe & certain in C:

int a=4,*ptr=&a;  //Statement 1

x+=4,y=x*2;  //Statement 2  (Assume x and y are integer variables)

如果是,怎么办?尤其是在第二种情况下,如果逗号不是序列点,那么在将赋值y=x*2用于赋值y=x*2之前,如何确保将x递增了4?对于第一条语句,在将其地址分配给ptr之前,如何确定a已被初始化并分配了内存?我应该放心使用,并在上面使用以下内容:

If yes, how? Especially in the second case, if a comma is not a sequence point, how can we be sure x has been incremented by 4 before we use it in the assignment y=x*2? And for the first statement, how can I be sure a has been initialized and allocated memory before I assign its address to ptr? Should I play safe and use the following for the above:

int a=4,*ptr;
ptr=&a;

x+=4;
y=x*2;

编辑我对逗号运算符的了解告诉我,这些陈述是安全的.但是,在阅读了有关序列点以及如何定义诸如printf("%d",a++,++a,a++)之类的内容之后,我有了第二个想法.

Edit My understanding of the comma operator tells me that those statements are safe. But after reading about sequence points and how something like printf("%d",a++,++a,a++) is undefined, I am having second thoughts.

推荐答案

在函数调用中分隔函数参数的逗号不是 逗号运算符-只是标点符号恰好在与逗号运算符相同的方式.评估不同的函数参数之间没有顺序点,因此这就是为什么在这种情况下获得UB的原因.

The comma that separates function arguments in a function call is not a comma operator - it's just punctuation that happens to be spelled in the same way as the comma operator. There's no sequence point between the evaluation of different function arguments, so that's why you get UB in that case.

另一方面,在您的表情中:

On the other hand, in your expression:

x+=4,y=x*2;

这里的逗号逗号运算符,它引入了序列点;没有UB.

the comma here is a comma operator, which introduces a sequence point; there's no UB.

在声明中,声明符之间的逗号也不是逗号运算符;但是,完整声明符(一个声明符不属于另一个声明符的一部分)的末尾确实会引入一个序列点,因此声明如下:

In a declaration, the comma between declarators is also not a comma operator; however, the end of a full declarator (a declarator not part of another declarator) does introduce a sequence point, so a declaration like:

int a = 2, b = a + 1;

不是UB.

这篇关于是否有“顺序点"?像"int a = 4,* ptr =& a;"这样的语句会出现问题或"x + = 4,y = x * 2;"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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