本文介绍了混淆宏扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

可能重复:结果
  Could谁能解释这些不确定的行为,(I = I + + + + I,I = I + +,等...)结果
  What问题可能下面的宏带来的应用程序吗?

我写了宏扩展的示例应用程序在我的iOS实现它(目标C code)。

I wrote a sample application with macro expansion for implementing it in my iOS (Objective C code).

这是一样的东西:

#define SQUARE(x) ( x * x )
main( )
{
   int i = 3, j, k ;
   j = SQUARE( i++ ) ;
   k = SQUARE( ++i ) ;
   printf ( "\nValue of i++ = %d\nValue of ++i = %d", j, k ) ;
}

产量为:

Value of i++ = 9
Value of ++i = 49

预计产量为:

 Value of i++ = 9
 Value of ++i = 25

我对这个结果感到惊讶。我有点困惑与此宏扩展。

I'm surprised by this result. I'm little bit confused with this macro expansion.

为什么会发生?请帮我找到了原因。

Why did it happen? Please help me to find the reason.

推荐答案

这是真的不确定的行为(而不应依赖于另外一个编译器,甚至相同的编译器的下一次运行)自它增加了同一个变量两次没有顺序点相同的语句,但这是什么,似乎在这种情况下发生的;

This is really undefined behavior (and should not be relied on on another compiler or even the next run of the same compiler) since it increases the same variable twice in the same statement without a sequence point, but this is what seems to happen in this case;

#define SQUARE(x) ( x * x )
j = SQUARE( i++ ) ;
k = SQUARE( ++i ) ;

将扩大到

j = (i++ * i++);     // Compiler could do i * i, then increment twice, or use i * (i + 1), or...

k = (++i * ++i);     // Same as above

由于这是不确定的行为,编译器可以自由地为所欲为(GCC的旧版本制作的节目推出nethack ;-)。为了更precise:编译器是免费的假设未定义的行为永远不会被要求,只是确保code正常工作在正常情况。会发生什么意外的情况下,是任何人的选择。

As this is undefined behaviour, the compiler is free to do whatever it wants (old versions of gcc made the program launch nethack ;-). To be more precise: The compiler is free to assume undefined behaviour won't ever be called upon, and just make sure the code works correctly in "normal" cases. What happens in unexpected cases is anybody's bet.

这篇关于混淆宏扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:28