C语言函数调用时的传参操作在32位x86机器上依赖栈进行.而在x86_64的机器上使用了部分寄存器作为辅助,但如果参数过多,寄存器不够使用,此时也必须借助于栈操作实现传参.尽管C语言对函数传递参数的个数没有明确限制(依编译器实现而定:http://stackoverflow.com/questions/9034787/function-parameters-max-number),但过多的参数传递势必影响代码执行效率.
通常C语言函数传参是非常明确的,如下面这个函数:
int test(int a,float b,int *pointer);
注:以下例子均使用32位x86,gcc编译器说明.
但如果把数组或结构体作为参数传递呢?到底传递了多少参数,占用了多少栈空间?
typedef struct list{
int a;
int b;
int c;
int d;
int e;
}list;
int test1(int array[5]);
int test2(list listtest);
先看数组的例子:
/*passing arguments test:array*/
#include<stdio.h>
void array(int tmp[5]){
printf("%d\n",tmp[2]);
}
int main(void){
int test[]={1,2,3,4,5};
array(test);
}
编译成汇编代码,先截取main函数传参部分:
movl $1, -32(%ebp)
movl $2, -28(%e