简体   繁体   English

函数反向传递参数

[英]Function passing arguments in reverse

Here is my function: 这是我的功能:

void abc(char  *def, unsigned int w, unsigned int x, unsigned int y, unsigned int z)
{
   printf("val 1 : %d\n", w);
   printf("val 2 : %d\n", x);
   printf("val 3 : %d\n", y);
   printf("val 4 : %d\n", z);
}

and here is where I call this function: 这是我调用此函数的地方:

unsigned int exp[4] = { 1, 2, 3, 4 };
unsigned short count = 0;
abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]);

and here is the output that I expect: 这是我期望的输出:

val1 : 1
val2 : 2
val3 : 3
val4 : 4

but what I get is completely reverse of it: 但我得到的完全相反:

val1 : 4
val2 : 3
val3 : 2
val4 : 1

I don't know why? 我不知道为什么? Any help would be appreciated. 任何帮助,将不胜感激。

From standard docs, 5.4 从标准文档, 5.4

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified58) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. 除非另有说明,否则单个运算符的操作数和各个表达式的子表达式的评估顺序以及副作用发生的顺序是未指定的.58)在前一个和下一个序列点之间,标量对象的存储值最多应修改一次通过表达式的评价。 Furthermore, the prior value shall be accessed only to determine the value to be stored. 此外,只能访问先前值以确定要存储的值。 The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; 对于完整表达式的子表达式的每个允许排序,应满足本段的要求; otherwise the behavior is undefined. 否则行为未定义。

An example from the Standard docs itself, 标准文档本身的一个例子,

i = v[i ++]; / / the behavior is undefined

And it is for the very same reason that 这也是出于同样的原因

abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]); is undefined.. 未定义..

You should not use the ++ operator, operating on the same variable, more than once in the same statement. 您不应该在同一语句中多次使用对同一变量进行操作的++运算符。 The order in which the operation will be performed is not defined. 未定义执行操作的顺序。

Try: 尝试:

abc(anyarray, exp[count], exp[count+1], exp[count+2], exp[count+3]);  
count += 4; 

您通过在没有插入序列点的情况下多次修改count来调用未定义的行为。

You are counting on the parameters being evaluated left to right. 您指望从左到右评估的参数。 You can't make any assumptions about the order that they're evaluated. 您不能对它们被评估的顺序做出任何假设。 In this case, it looks like the compiler is evaluating them right-to-left. 在这种情况下,看起来编译器正在从右到左对它们进行评估。

Also, you may want to look up sequence points, because it may be that you shouldn't use the ++ operator in this way. 此外,您可能希望查找序列点,因为您可能不应该以这种方式使用++运算符。

abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]);

The order of evaluation of arguments of abc is unspecified but the expression invokes undefined behaviour because you are trying to modify a variable count more than once between two sequence points. abc的参数的评估顺序未指定,但表达式调用未定义的行为,因为您试图在两个序列点之间多次修改变量count

Furthermore using incorrect format specifier in printf() also invokes UB. 此外,在printf()使用不正确的格式说明符也会调用UB。 Please make sure you have used correct format specifiers(ie %u for unsigned int ) in printf() . 请确保在printf()使用了正确的格式说明符(即%u表示unsigned int printf()

You got this because you called adb(exp,a,b,c,d) according to your problem, but during call of function d is pushed first on stack and then c ,b relatively. 你得到这个是因为你根据你的问题调用了adb(exp,a,b,c,d) ,但是在函数调用期间d首先在堆栈上推送然后c ,b相对。 As you passed exp[count++] at last argument which will process first to push over stack means 1 is pushed first then 2 then 3 then 4. And in called function pop performed so you get w=4 x=3 y=2 z=1 that's it. 当您在最后一个参数处通过exp[count++]时,它将首先处理以推送堆栈意味着首先按下1然后按2然后按3然后按4.然后在调用函数pop中执行,这样你得到w=4 x=3 y=2 z=1就是这样。

This is because of the calling convention. 这是因为调用约定。 In _cdecl, the default calling convention for c/c++ programs (according to microsoft), the parameters are passed on the stack to the function in reverse order. 在_cdecl中,c / c ++程序的默认调用约定(根据microsoft),参数以相反的顺序在堆栈上传递给函数。 Because of this, the parameters are also evaluated in reverse order. 因此,也以相反的顺序评估参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM