Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
职能范围及;递增参数 一个示例问题要求我们考虑下面的代码并预测函数将被打印出来的函数函数t1 < /> >: void func_1(int i, int j) { printf("i is %d, j is %d\n", i, j); } /* ... */ /* somewhere in the code, a call to func_1 */ int i = 30; func_1(i, i++); /* ... */_C_Function_Scope - Fatal编程技术网

职能范围及;递增参数 一个示例问题要求我们考虑下面的代码并预测函数将被打印出来的函数函数t1 < /> >: void func_1(int i, int j) { printf("i is %d, j is %d\n", i, j); } /* ... */ /* somewhere in the code, a call to func_1 */ int i = 30; func_1(i, i++); /* ... */

职能范围及;递增参数 一个示例问题要求我们考虑下面的代码并预测函数将被打印出来的函数函数t1 < /> >: void func_1(int i, int j) { printf("i is %d, j is %d\n", i, j); } /* ... */ /* somewhere in the code, a call to func_1 */ int i = 30; func_1(i, i++); /* ... */,c,function,scope,C,Function,Scope,我认为当参数以递增的形式传递时,不可能预测编译器何时递增I。然而,解决办法是: The values in the argument are passed as an attack to the function, hence 'j' receives a value '30' and then i receives the incremented value which is '31'. Output: i is 31, j is 30 有人能解释一下什么是对函数的攻击以及这种攻击是如何

我认为当参数以递增的形式传递时,不可能预测编译器何时递增I。然而,解决办法是:

The values in the argument are passed as an attack to the function, hence 'j' receives 
a value '30' and then i receives the incremented value which is '31'.

Output: i is 31, j is 30

有人能解释一下什么是对函数的攻击以及这种攻击是如何发生的吗?

这不是不可能预测的;编译器以确定性的方式工作,即使在规范未涵盖或覆盖不好的灰色区域。在这种情况下,特定的编译器参数会从右向左推送,而post增量会在推送右参数后不久发生。

解决方案通常是错误的。你是对的;代码的行为尚未定义。在某些编译器上,答案可能是30和31;在其他情况下,可能是30和30;在其他情况下,可能是31和31;其他人可能只是删除硬盘上的所有文件(因为未定义的行为是未定义的)。幸运的是,在编译器中,清除故障行为的所有痕迹相对来说是不太可能的

对于特定平台上的特定编译器,解决方案可能是正确的



实际上,我认为在
func_1()
j
不可能得到31,但是产生30和30的操作序列很容易想象:将
I
的值推两次,然后增加I,然后调用函数。

谢谢,但为什么是从右向左还是从左向右?当我读代码时,在我看来,
I=30
并且在递增
I
之后,
j=31
。现在我完全糊涂了。问题并不是说我们要假设一个特定的平台,而是特别指出
i是31
j是30
=/@拉芬·阮:对不起,你弄糊涂了。问题在于,这个问题不是很好——像询问未定义的行为那样提问。答案也不是很好——只说明一个特定(但尚未说明)编译器最多表现出的行为。不幸的是,MacOS X 10.6.7上的GCC 4.6.0 set-Fusy给出了“警告:对‘i’的操作可能未定义”和结果“i为31,j为30”,因此我没有证明我的观点。@raphnguyen:我已经在Solaris 10(SPARC)上进行了检查;这就产生了31/30。我查过HP-UX11.23;这就产生了30/30。我检查了AIX6.1;这就产生了30/30。啊,糟糕。明天考试之前,我得请我的教授澄清一下。非常感谢你试图澄清这一困惑!