函数参数压栈顺序2

1、先来看一个例子:

#include "stdio.h" 

int main(int argc, char* argv[]) 
{ 
    int i = 0;
    printf("%d, %d\n", ++i , ++i);

    return 0; 
} 
//vc编译输出           2,1
//xcode使用的编译器输出  1,2

这还是跟编译器的默认调用约定有关,vc编译器的默认调用约定的函数参数压栈顺序是从右向左的,所以先计算最右边的++i;xcode使用的编译器则相反。

2、继续例子

#include "stdio.h" 

int f()
{
    printf("ffff\n");

    return 1;
}


int g()
{
    printf("gggg\n");

    return 2;
}


int main(int argc, char* argv[]) 
{ 
    int i = 0;
    printf("%d, %d\n", f() , g());

    return 0; 
} 

vc:gggg,ffff

xcode:ffff,gggg

3、继续

#include "stdio.h" 

void test(int a,int b)
{
    printf("%p\n", &a);
    printf("%p\n", &b);
}


int main(int argc, char* argv[]) 
{ 
    test(5,6);
    return 0; 
} 

vc:  0x00000004, 0x00000008

xcode:0x00000008, 0x00000004

原文地址:https://www.cnblogs.com/zzj2/p/3025216.html