麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C > 正文

c語言:基于函數指針的兩個示例分析

2020-01-26 16:14:12
字體:
來源:轉載
供稿:網友

第一個:
------------------------------------------------------

復制代碼 代碼如下:

#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(): %p/n", f);
   else
      printf("Function address: %p/n", 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(): %p/n", 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的位置指向同一位置?

后來別人通過反匯編發現的情況:
==============================================
如下紅色的幾行,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(): %p/n"
.LC1:
.string "Function address: %p/n"
.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("%0x/t%0x/n",f,strcmp); //看這里,輸出f與strcmp的地址是相同的
004010A8 push offset strcmp (004011a0)
004010AD mov eax,dword ptr [ebp+8]
004010B0 push eax
004010B1 push offset string "%0x/t%0x/n" (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(): %0x/n", f);
004010C7 mov ecx,dword ptr [ebp+8]
004010CA push ecx
004010CB push offset string "Address of strcmp(): %0x/n" (00422044)
004010D0 call printf (00401120)
004010D5 add esp,8
16: else
004010D8 jmp tell_me+5Bh (004010eb)
17: printf("Function address: %p/n", f);
004010DA mov edx,dword ptr [ebp+8]
004010DD push edx
004010DE push offset string "Function address: %p/n" (00422028)
004010E3 call printf (00401120)
004010E8 add esp,8=======================================================

第二個:
--------------------------------------------------------------------------------------------

復制代碼 代碼如下:

#include <stdio.h>
#include <string.h>
int main(void)
{
   char p1[20] = "abc", *p2 = "pacific sea";
   printf("%s %s %s/n", 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("%s/t%s/t%s/n", 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 "%s/t%s/t%s/n" (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
我更傾向于第二種.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 性少妇chinesevideo | 一级尻逼视频 | 粉嫩粉嫩一区二区三区在线播放 | 日韩a毛片免费观看 | 91短视频在线免费观看 | 欧美淫交 | 欧美性视频一区二区 | 亚洲一区动漫 | 国产精品爱久久久久久久 | 伊人在线视频 | 国产小视频在线观看 | 爱唯侦察 国产合集 亚洲 | 双性精h调教灌尿打屁股的文案 | 人人做人人看 | 天天夜干| 777sesese| 成人在线视频免费播放 | 激情网站在线观看 | 欧美一级一区二区三区 | 久久综合狠狠综合久久 | 国产瑟瑟视频 | 亚洲免费毛片基地 | 一级做a爱片久久 | 国产又白又嫩又紧又爽18p | 在线日韩av电影 | 国产女同疯狂激烈互摸 | 97中文字幕第一一一页 | 视屏一区 | 欧美性黄 | 羞羞答答视频 | 草草视频免费 | 一级成人欧美一区在线观看 | 欧美成人三级大全 | 国产一区二区久久精品 | 黄色特级视频 | 亚洲一区在线免费视频 | 北京一级毛片 | 国产成人自拍av | 成年性羞羞视频免费观看无限 | 日本黄色大片免费 | 欧美一级黄色录相 |