c语言:基于函数指针的两个示例分析

2016-02-19 09:06 2 1 收藏

有了下面这个c语言:基于函数指针的两个示例分析教程,不懂c语言:基于函数指针的两个示例分析的也能装懂了,赶紧get起来装逼一下吧!

【 tulaoshi.com - 编程语言 】

第一个:
------------------------------------------------------
代码如下:

#include stdio.h
#include string.h
void tell_me(int f(const char *, const char *));
int main(void)
{
   tell_me(strcmp);
   tell_me(main);
   return 0;
}
void tell_me(int f(const char *, const char *))
{
   if (f == strcmp) /* -----我不理解这里*/
      printf("Address of strcmp(): %pn", f);
   else
      printf("Function address: %pn", f);
}

--------------------------------------------------------------
其中我不理解的是,这个程序表达的应该是说f是一个指向函数的指针,判断的时候是判断f是否指向函数strcmp,如果是的话,就输出strcmp这个函数的地址.如果不是,就输出main函数的地址
因为函数名可以作为指针,所以if (f == strcmp)应该是说判断2个指针的地址是否相同对吧?
我用gdb 断点到此,print f和printfstrcmp得到的是不同的地址啊,并且可以发现f和*f的内容居然一样,strcmp和*strcmp也一样,请问是什么原因,如何解释?

(gdb) print f
$1 = (int (*)(const char *, const char *)) 0x8048310 strcmp@plt
(gdb) print strcmp
$2 = {text variable, no debug info} 0xb7e59d20 strcmp
(gdb) n
16 printf("Address of strcmp(): %pn", f);
(gdb) print strcmp
$3 = {text variable, no debug info} 0xb7e59d20 strcmp
(gdb) print *strcmp
$4 = {text variable, no debug info} 0xb7e59d20 strcmp
(gdb) print *f
$5 = {int (const char *, const char *)} 0x8048310 strcmp@plt
(gdb) n
Address of strcmp(): 0x8048310
19 }
(gdb) n
后来我查到plt是指的过程链接表,是不是说只有在执行到f == strcmp时候,才把f的地址和strcmp的位置指向同一位置?

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

后来别人通过反汇编发现的情况:
==============================================
如下红色的几行,main与strcmp此时为常量(你也会发现没有.data段),在汇编代码中他是把这两个常量写入函数堆栈,然后调用函数,作出对比,然后输出。而你所说的 f ,也就是函数参数,实际上它只作为预分配的参考(汇编代码中,你是找不到 f 的)。
-------------------------------------------------------------------------------------
.file "1.c"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $4, %esp
movl $strcmp, (%esp)
call tell_me
movl $main, %eax
movl %eax, (%esp)
call tell_me
movl $0, %eax
addl $4, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.section .rodata
.LC0:
.string "Address of strcmp(): %pn"
.LC1:
.string "Function address: %pn"
.text
.globl tell_me
.type tell_me, @function
tell_me:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
cmpl $strcmp, 8(%ebp)
jne .L4
movl 8(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
jmp .L6
.L4:
movl 8(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call printf
.L6:
leave
ret
.size tell_me, .-tell_me
.ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
.section .note.GNU-stack,"",@progbits
==================================================
00401090 push ebp //第一题的反汇编
00401091 mov ebp,esp
00401093 sub esp,40h
00401096 push ebx
00401097 push esi
00401098 push edi
00401099 lea edi,[ebp-40h]
0040109C mov ecx,10h
004010A1 mov eax,0CCCCCCCCh //应该说在函数传递时,f与strcmp的地址都相同
004010A6 rep stos dword ptr [edi]
13: printf("%0xt%0xn",f,strcmp); //看这里,输出f与strcmp的地址是相同的
004010A8 push offset strcmp (004011a0)
004010AD mov eax,dword ptr [ebp+8]
004010B0 push eax
004010B1 push offset string "%0xt%0xn" (0042201c)
004010B6 call printf (00401120)
004010BB add esp,0Ch
14: if (f == strcmp) /* -----我不理解这里*/ //比较后,输出的地址同样是一样的,
004010BE cmp dword ptr [ebp+8],offset strcmp (004011a0)
004010C5 jne tell_me+4Ah (004010da)
15: printf("Address of strcmp(): %0xn", f);
004010C7 mov ecx,dword ptr [ebp+8]
004010CA push ecx
004010CB push offset string "Address of strcmp(): %0xn" (00422044)
004010D0 call printf (00401120)
004010D5 add esp,8
16: else
004010D8 jmp tell_me+5Bh (004010eb)
17: printf("Function address: %pn", f);
004010DA mov edx,dword ptr [ebp+8]
004010DD push edx
004010DE push offset string "Function address: %pn" (00422028)
004010E3 call printf (00401120)
004010E8 add esp,8=======================================================

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

第二个:
--------------------------------------------------------------------------------------------
代码如下:

#include stdio.h
#include string.h
int main(void)
{
   char p1[20] = "abc", *p2 = "pacific sea";
   printf("%s %s %sn", p1, p2, strcat(p1, p2)); /*-----问题出在这里*/
   return 0;
}

---------------------------------------------------------------------------------------------
输出我的认为应该是先输出p1, p2以后再执行strcat. 但是实际输出的情况:
abcpacific sea pacific sea abcpacific sea
可以发现strcat先于p1执行,改变了p1的内容,请问这个内部是怎么的顺序?难道printf不是顺序执行的?
=======================================================

得到的解答:
不同的编译器printf的函数参数进栈顺序不同,printf函数中的strcat可以看反汇编代码.
6: printf("%st%st%sn", p1, p2, strcat(p1, p2)); /*-----问题出在这里*/
00401045 mov edx,dword ptr [ebp-18h]
00401048 push edx
00401049 lea eax,[ebp-14h]
0040104C push eax
0040104D call strcat (00401130) //可以看到是先调用strcat函数的,
00401052 add esp,8
00401055 push eax
00401056 mov ecx,dword ptr [ebp-18h]
00401059 push ecx
0040105A lea edx,[ebp-14h]
0040105D push edx
0040105E push offset string "%st%st%sn" (0042201c)
00401063 call printf (004010a0) //最后调用printf函数输出
00401068 add esp,10h
===============================================
汇编直观而简单的说明了一些疑问....
再说下gcc如何产生汇编代码:
1: gcc -S main.c 可以生成
2: gcc -c main.c 生成main.o
objdump -S -D main.o main.asm
我更倾向于第二种.

来源:https://www.tulaoshi.com/n/20160219/1589568.html

延伸阅读
标签: ASP
  (原创 vince6799) 在asp代码中分页是有点麻烦的事情,个人在在代码编写过程中把分页代码写成了两个函数,虽然在功能上不是很完善,但对于一般的应用应该是满足的了。 <% '分页函数分为两个函数 'CalcPage(totalrec,msg_per_page,currentpage,n,rowcount,PageRs) 分页计算函数 'PageList(ListType,url,querry,Separator,ListLink...
标签: ASP
'****************************** '||Function TimeDiff(sBegin, sEnd) '||本函数计算两个时间的差,可以不用更改直接使用 '||作者:machinecat 2001/10/26 '****************************** '****************************** '注:首先需要判断用户输入的sBegin与sEnd之间的大小 '可以通过DataDiff函数获得两者之间的时间差,不需要进行复杂...
标签: PHP
  <?php //文件名:date.inc.php3 //在使用这两个函数前,要先将日期或日期时间转换成timestamp类型。 //如: //$today=mktime(0,0,0,date("m"),date("d"),date("Y")); /****模拟sqlserver中的dateadd函数******* $part 类型:string 取值范围:year,month,day,hour,min,sec 表示:要增加的日期的哪个...
一个代码: 代码如下: #includestdio.h #includestdlib.h #define uchar unsigned char #define uint unsigned int void display(uchar *p); char h[4] = {'A','B','C','\0'}; char e[4] = {'E','F','L','\0'}; char l[4] = {'M','N','O','\0'}; char o[4] = {'X','Y','Z','\0'}; int main(void) {     int i;  &nbs...
  // test12.cpp : Defines the entry point for the console application.   //   #include "stdafx.h"   void func(int i)   {   printf("This is for test %i", i);   } !-- frame contents -- !-- /frame contents --   typedef void (*P...

经验教程

478

收藏

30
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部