|
发表于 2004-8-30 08:57:41
|
显示全部楼层
一个用struct传参数的小程序
- typedef struct{
- int arr[10];
- }data_t;
- void func(data_t data)
- {
- return;
- }
- int main()
- {
- data_t d;
- func(d);
- return 0;
- }
复制代码
用gcc-3.2.2编译:
- .file "main.c"
- .text
- .globl func
- .type func,@function
- func:
- pushl %ebp
- movl %esp, %ebp
- leave
- ret
- .Lfe1:
- .size func,.Lfe1-func
- .globl main
- .type main,@function
- main:
- pushl %ebp
- movl %esp, %ebp
- pushl %edi
- pushl %esi
- subl $48, %esp /* 在堆栈中分配局部变量d的空间 */
- andl $-16, %esp
- movl $0, %eax
- subl %eax, %esp
- subl $8, %esp
- subl $40, %esp /* 在堆栈中分配参数的空间 */
- movl %esp, %edi
- leal -56(%ebp), %esi
- cld
- movl $10, %eax
- movl %eax, %ecx
- rep
- movsl /* 把d的内容拷贝到参数空间 */
- call func
- addl $48, %esp
- movl $0, %eax
- leal -8(%ebp), %esp
- popl %esi
- popl %edi
- leave
- ret
- .Lfe2:
- .size main,.Lfe2-main
- .ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
复制代码
显然用struct传参数是一个很浪费堆栈的做法。
我记得在Windows下用VC6编译的结果与此非常不同,并且当struct空间比较小和比较大的时候处理方法也有所不同,有兴趣的兄弟可以试一下。 |
|