【求助】关于取函数返回值地址的问题
遇到一个问题,请大侠们指点
以一个代码为例:
#include <iostream>
#include <string.h>
int getName()
{
int i = 20;
return i;
}
int main()
{
int *p;
p = &getName();
return 0;
}
ClassName getVal()
{
.....
}
int main()
{
ClassName *name;
name = &getVal();
...
return 0;
}
2 #include <stdio.h>
3 #include <string.h>
4 int getName()
5 {
6 int i = 20;
7 return i;
8 }
9 int main()
10 {
11 int *p;
12 *p = getName();
13 return 0;
14 }
80483b4 <getName>:
119 80483b4: 55 push %ebp
120 80483b5: 89 e5 mov %esp,%ebp//
121 80483b7: 83 ec 10 sub $0x10,%esp
122 80483ba: c7 45 fc 14 00 00 00 movl $0x14,-0x4(%ebp)
123 80483c1: 8b 45 fc mov -0x4(%ebp),%eax//here1
/*很显然here1它把返回值放到了寄存器eax中,然后有主函数中here2存到对应的变量中去。
也就说这个操作执行后,函数getname中局部变量-0x4(%ebp),已经不再重要了,
因为它的值已经转移走了。系统可以用他干别的事情了。而且它是在栈里面的,局部于%ebp
在进入函数的时候push %ebp保存调用函数地址;mov %esp,%ebp转向被调用函数栈指针*/
124 80483c4: c9 leave
125 80483c5: c3 ret
126
127 080483c6 <main>:
128 80483c6: 55 push %ebp
129 80483c7: 89 e5 mov %esp,%ebp
130 80483c9: 83 ec 10 sub $0x10,%esp
131 80483cc: e8 e3 ff ff ff call 80483b4 <getName>
132 80483d1: 8b 55 fc mov -0x4(%ebp),%edx
133 80483d4: 89 02 mov %eax,(%edx)//here2
134 80483d6: b8 00 00 00 00 mov $0x0,%eax
135 80483db: c9 leave
136 80483dc: c3 ret
137 80483dd: 90 nop
138 80483de: 90 nop
139 80483df: 90 nop