我最近在C语言中有一些函数指针的经验。
因此,按照回答自己问题的传统,我决定对最基本的问题做一个简短的总结,以供那些需要快速切入主题的人使用。
我最近在C语言中有一些函数指针的经验。
因此,按照回答自己问题的传统,我决定对最基本的问题做一个简短的总结,以供那些需要快速切入主题的人使用。
当前回答
C中的函数指针
让我们从一个我们将要指向的基本函数开始:
int addInt(int n, int m) {
return n+m;
}
首先,让我们定义一个指向接收2个int并返回一个int的函数的指针:
int (*functionPtr)(int,int);
现在我们可以安全地指出我们的功能:
functionPtr = &addInt;
现在我们有一个指向函数的指针,让我们使用它:
int sum = (*functionPtr)(2, 3); // sum == 5
将指针传递给另一个函数基本上是相同的:
int add2to3(int (*functionPtr)(int, int)) {
return (*functionPtr)(2, 3);
}
我们也可以在返回值中使用函数指针(尽量跟上,这会变得很混乱):
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
但使用typedef要好得多:
typedef int (*myFuncDef)(int, int);
// note that the typedef name is indeed myFuncDef
myFuncDef functionFactory(int n) {
printf("Got parameter %d", n);
myFuncDef functionPtr = &addInt;
return functionPtr;
}
其他回答
C中的函数指针
让我们从一个我们将要指向的基本函数开始:
int addInt(int n, int m) {
return n+m;
}
首先,让我们定义一个指向接收2个int并返回一个int的函数的指针:
int (*functionPtr)(int,int);
现在我们可以安全地指出我们的功能:
functionPtr = &addInt;
现在我们有一个指向函数的指针,让我们使用它:
int sum = (*functionPtr)(2, 3); // sum == 5
将指针传递给另一个函数基本上是相同的:
int add2to3(int (*functionPtr)(int, int)) {
return (*functionPtr)(2, 3);
}
我们也可以在返回值中使用函数指针(尽量跟上,这会变得很混乱):
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
但使用typedef要好得多:
typedef int (*myFuncDef)(int, int);
// note that the typedef name is indeed myFuncDef
myFuncDef functionFactory(int n) {
printf("Got parameter %d", n);
myFuncDef functionPtr = &addInt;
return functionPtr;
}
“从头开始”功能具有“开始执行的内存地址”。在汇编语言中,它们被称为(调用“函数的内存地址”)。现在回到C。如果函数有内存地址,那么它们可以由C中的指针操作
1.首先需要声明一个指向函数的指针2.传递所需函数的地址
****注意->函数应为相同类型****
这个简单的计划将说明一切。
#include<stdio.h>
void (*print)() ;//Declare a Function Pointers
void sayhello();//Declare The Function Whose Address is to be passed
//The Functions should Be of Same Type
int main()
{
print=sayhello;//Addressof sayhello is assigned to print
print();//print Does A call To The Function
return 0;
}
void sayhello()
{
printf("\n Hello World");
}
之后,让我们来看看机器是如何理解它们的。32位体系结构的上述程序的机器指令一瞥。
红色标记区域显示地址如何在eax中交换和存储。然后是eax上的调用指令。eax包含函数所需的地址。
C中函数指针的一个主要用途是调用在运行时选择的函数。例如,C运行时库有两个例程,qsort和bsearch,它们获取一个指向函数的指针,该函数被调用来比较正在排序的两个项目;这允许您根据希望使用的任何条件分别对任何内容进行排序或搜索。
一个非常基本的例子是,如果有一个名为print(int x,int y)的函数,反过来可能需要调用一个函数(add()或sub(),它们是相同类型的),那么我们将做什么,我们将向print()函数添加一个函数指针参数,如下所示:
#include <stdio.h>
int add()
{
return (100+10);
}
int sub()
{
return (100-10);
}
void print(int x, int y, int (*func)())
{
printf("value is: %d\n", (x+y+(*func)()));
}
int main()
{
int x=100, y=200;
print(x,y,add);
print(x,y,sub);
return 0;
}
输出为:
值为:410值为:390
我最喜欢的函数指针用途之一是迭代器-
#include <stdio.h>
#define MAX_COLORS 256
typedef struct {
char* name;
int red;
int green;
int blue;
} Color;
Color Colors[MAX_COLORS];
void eachColor (void (*fp)(Color *c)) {
int i;
for (i=0; i<MAX_COLORS; i++)
(*fp)(&Colors[i]);
}
void printColor(Color* c) {
if (c->name)
printf("%s = %i,%i,%i\n", c->name, c->red, c->green, c->blue);
}
int main() {
Colors[0].name="red";
Colors[0].red=255;
Colors[1].name="blue";
Colors[1].blue=255;
Colors[2].name="black";
eachColor(printColor);
}
被解雇的指南:如何在x86机器上通过手工编译代码滥用GCC中的函数指针:
这些字符串文字是32位x86机器代码的字节。0xC3是x86 ret指令。
你通常不会用手工编写这些,你会用汇编语言编写,然后使用像nasm这样的汇编程序将其汇编成一个平面二进制,然后将其十六进制转储成一个C字符串文字。
返回EAX寄存器上的当前值int eax=((int(*)())(“\xc3<-返回eax寄存器的值”)();编写交换函数整数a=10,b=20;((void(*)(int*,int*))“\x8b\x44\x04\x8b\x5c\x24\x08\x80\x80\x8b\ux1b\x31\x31\xc3\xcb\x4c\x24\x04\x09\x01\xcb\x4c\x04\x08\x49\xc3<-这将交换a和b的值”)(-a,-b);将for循环计数器写入1000,每次调用一些函数((int(*)())“\x66\x31\xc0\x8b\x5c\x24\x64\x66\x40\x50\xxf\xd3\x58\x66\x3d\xC8\x03\x75\xf4\xc3”)(函数);//调用函数1->1000您甚至可以编写一个计数为100的递归函数const char*lol=“\x8b\x5c\x24\x4\x3\x8\x3\x0\x0\x7e\x2\x31\x80\x83\x48\x64\x7d\x6\x40\x53\xdf\xd3\x5b\xc3\xc3<-递归调用地址lol处的函数。”;i=((int(*)())(lol))(lol);
请注意,编译器将字符串文本放在.rodata部分(或Windows上的.rata),该部分作为文本段的一部分链接(以及函数代码)。
文本段具有Read+Exec权限,因此将字符串文本转换为函数指针是有效的,而无需像动态分配内存那样进行mprotect()或VirtualProtect()系统调用。(或者gcc-z execstack将程序与堆栈+数据段+堆可执行文件链接起来,作为快速破解。)
要反汇编这些,您可以编译它以在字节上添加标签,并使用反汇编程序。
// at global scope
const char swap[] = "\x8b\x44\x24\x04\x8b\x5c\x24\x08\x8b\x00\x8b\x1b\x31\xc3\x31\xd8\x31\xc3\x8b\x4c\x24\x04\x89\x01\x8b\x4c\x24\x08\x89\x19\xc3 <- This swaps the values of a and b";
用gcc-c-m32-foo.c编译,用objdump-D-rwC-Mintel反汇编,我们可以得到程序集,并发现该代码通过破坏EBX(一个调用保留寄存器)而违反了ABI,通常效率很低。
00000000 <swap>:
0: 8b 44 24 04 mov eax,DWORD PTR [esp+0x4] # load int *a arg from the stack
4: 8b 5c 24 08 mov ebx,DWORD PTR [esp+0x8] # ebx = b
8: 8b 00 mov eax,DWORD PTR [eax] # dereference: eax = *a
a: 8b 1b mov ebx,DWORD PTR [ebx]
c: 31 c3 xor ebx,eax # pointless xor-swap
e: 31 d8 xor eax,ebx # instead of just storing with opposite registers
10: 31 c3 xor ebx,eax
12: 8b 4c 24 04 mov ecx,DWORD PTR [esp+0x4] # reload a from the stack
16: 89 01 mov DWORD PTR [ecx],eax # store to *a
18: 8b 4c 24 08 mov ecx,DWORD PTR [esp+0x8]
1c: 89 19 mov DWORD PTR [ecx],ebx
1e: c3 ret
not shown: the later bytes are ASCII text documentation
they're not executed by the CPU because the ret instruction sends execution back to the caller
在Windows、Linux、OS X等系统上,这段机器代码(可能)可以在32位代码中工作:所有这些OS上的默认调用约定都在堆栈中传递参数,而不是在寄存器中更有效地传递参数。但EBX在所有正常的调用约定中都保留了调用,因此将其用作临时寄存器而不保存/恢复它很容易使调用者崩溃。