手机版
你好,游客 登录 注册
背景:
阅读新闻

从汇编的角度分析C语言

函数指针与指针的指针

[日期:2012-02-20] 来源:Linux社区  作者:sg131971 [字体:大 中 小]

函数指针:

[cpp]
  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3.   
  4. void add()  
  5. {  
  6.   printf("12\n");  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     int * func;  
  12.     func = (int *)add;  
  13.     return 0;  
  14. }  
[html]
  1.     .file   "main.c"  
  2.     .section    .rodata  
  3. .LC0:  
  4.     .string "12"  
  5.     .text  
  6. .globl add  
  7.     .type   add, @function  
  8. add:  
  9.     pushl   %ebp  
  10.     movl    %esp, %ebp  
  11.     subl    $24, %esp  
  12.     movl    $.LC0, (%esp)  
  13.     call    puts  
  14.     leave  
  15.     ret  
  16.     .size   add, .-add  
  17. .globl main  
  18.     .type   main, @function  
  19. main:  
  20.     pushl   %ebp  
  21.     movl    %esp, %ebp  
  22.     subl    $16, %esp  
  23.     movl    $add, -4(%ebp)  
  24.     movl    $0, %eax  
  25.     leave  
  26.     ret  
  27.     .size   main, .-main  
  28.     .ident  "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"  
  29.     .section    .note.GNU-stack,"",@progbits  
[html]
  1. (gdb) disassemble main   
  2. Dump of assembler code for function main:  
  3.    0x080483c8 <+0>:     push   %ebp  
  4.    0x080483c9 <+1>:     mov    %esp,%ebp  
  5.    0x080483cb <+3>:     sub    $0x10,%esp  
  6.    0x080483ce <+6>:     movl   $0x80483b4,-0x4(%ebp)  
  7.    0x080483d5 <+13>:    mov    $0x0,%eax  
  8.    0x080483da <+18>:    leave    
  9.    0x080483db <+19>:    ret      
  10. End of assembler dump.  
  11. (gdb) p add  
  12. $1 = {<text variable, no debug info>} 0x80483b4 <add>  
  13. (gdb)   

指针的指针:

[cpp]
  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3.   
  4. void main()  
  5. {  
  6.     int a = 0x12;  
  7.     int *p1 = &a;  
  8.     int **p2 = &p1;  
  9.     int ***p3 = &p3;  
  10. }  
[html]
  1. (gdb) disassemble main   
  2. Dump of assembler code for function main:  
  3.    0x08048394 <+0>:     push   %ebp  
  4.    0x08048395 <+1>:     mov    %esp,%ebp  
  5.    0x08048397 <+3>:     sub    $0x10,%esp  
  6.    0x0804839a <+6>:     movl   $0x12,-0x8(%ebp)  
  7.    0x080483a1 <+13>:    lea    -0x8(%ebp),%eax  
  8.    0x080483a4 <+16>:    mov    %eax,-0xc(%ebp)  
  9.    0x080483a7 <+19>:    lea    -0xc(%ebp),%eax  
  10.    0x080483aa <+22>:    mov    %eax,-0x4(%ebp)  
  11.    0x080483ad <+25>:    lea    -0x10(%ebp),%eax  
  12.    0x080483b0 <+28>:    mov    %eax,-0x10(%ebp)  
  13.    0x080483b3 <+31>:    leave    
  14.    0x080483b4 <+32>:    ret      
  15. End of assembler dump.  
  16. (gdb)   
linux
【内容导航】
第1页:char *p与char p[]的区别 第2页:gcc代码优化
第3页:指针的赋值 第4页:函数的调用过程
第5页:函数指针与指针的指针 第6页:switch和if else效率分析
相关资讯       C语言  C 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款