| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1252 人关注过本帖
标题:子函数在return 前好好的,返回之后反而不好是为什么?
取消只看楼主 加入收藏
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
收藏
已结贴  问题点数:0 回复次数:3 
子函数在return 前好好的,返回之后反而不好是为什么?
程序代码:
void exit_stub(int status)
{
      char a[]="hello world";
      printf("exit_stub %d %p\n", status, &status);
      return ;   

}

int main(int argc,char** argv)
{
      printf("\ntest generic function 1\n");
    ContainAdd((void*)(exit), (void*)(exit_stub));
    printf("\ntest generic function 2\n");
    exit_stub(1);
    printf("\ntest generic function 3\n");
    RemoveAdd((void*)exit);
    ......
上述程序执行后显示神奇的错误:我的遗憾在于:
在exit_stub(1)返回之前的 printf("exit_stub %d %p\n", status, &status); 能正确执行,为什么返回之后就错误了呢 ?
图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: 函数 return 好好的 
2010-10-22 00:02
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
test generic function 11

test generic function 21

Breakpoint 3, exit_stub (status=1) at hooktest3.c:240
240          char a[]="hello world";
(gdb) c
Continuing.
exit_stub 1 0xbffff050, 0x80488c7

Program received signal SIGSEGV, Segmentation fault.
0x00000001 in ?? ()
(gdb) bt
#0  0x00000001 in ?? ()
#1  0xb7d41ace in __libc_start_main () from /lib/libc.so.6
#2  0x08048491 in _start () at ../sysdeps/i386/elf/start.S:119
(gdb)
这个是失败后,使用bt显示的信息,怎么有个  0x00000001 in ?? () 的函数地址这么小呢 ?


[ 本帖最后由 vfdff 于 2010-10-24 02:37 编辑 ]

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-10-24 02:36
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
这里有 _start (),好像_start ()是里会调用main的吧


(gdb) disas exit
Dump of assembler code for function exit@plt:
0x08048458 <exit@plt+0>:    jmp    *0x804a018
0x0804845e <exit@plt+6>:    push   $0x30
0x08048463 <exit@plt+11>:    jmp    0x80483e8
End of assembler dump.
另外,以上的反汇编能不能说明这个exit函数占用 6 + 11 = 17个字节吧?
(从 jmp    *0x804a018看出跳转指令需要 6 个字节,因此也假设jmp    0x80483e8需要6个字节)


[ 本帖最后由 vfdff 于 2010-10-24 23:37 编辑 ]

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-10-24 23:16
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
HCC:/home/zyd/projects # valgrind --tool=memcheck --leak-check=full --show-reachable=yes  ./test
==24939== Memcheck, a memory error detector.
==24939== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==24939== Using LibVEX rev 1884, a library for dynamic binary translation.
==24939== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==24939== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==24939== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==24939== For more details, rerun with: -v
==24939==
--24939-- DWARF2 CFI reader: unhandled CFI instruction 0:10
--24939-- DWARF2 CFI reader: unhandled CFI instruction 0:10

test generic function 11

test generic function 21
exit_stub 1 0xbe8eb050, 0x80488c7
==24939==
==24939== Process terminating with default action of signal 11 (SIGSEGV)
==24939==  Access not within mapped region at address 0x0
==24939==    at 0x1: ???
==24939==    by 0x4196ACD: (below main) (in /lib/libc-2.10.1.so)
==24939==  If you believe this happened as a result of a stack overflow in your
==24939==  program's main thread (unlikely but possible), you can try to increase
==24939==  the size of the main thread stack using the --main-stacksize= flag.
==24939==  The main thread stack size used in this run was 8388608.
==24939==
==24939== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
==24939== malloc/free: in use at exit: 0 bytes in 0 blocks.
==24939== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==24939== For counts of detected errors, rerun with: -v
==24939== All heap blocks were freed -- no leaks are possible.
Segmentation fault
HCC:/home/zyd/projects #

刚才使用valgrind工具打印出一些更详细的信息,但是原因我也还在做进一步分析,如果您有什么新的想法,也希望您能及时指教

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-10-25 00:20
快速回复:子函数在return 前好好的,返回之后反而不好是为什么?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017986 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved