钩子函数和回调函数个人理解-C语言

一句话概括:本质上都是对函数指针的调用。callback func是将函数作为参数传入, hook则是通过钩子注册函数操作作为全局变量的钩子指针来选择调用对象。目前我能感知到的区别就是业务隔离:callback的调用需要你知道你要传递什么功能实现到函数内部, 而如果不知道的话就可以用hook注册函数来操作。另外hook可以看作是一种已经内嵌入函数的为可拓展性(可通过注册新函数来达到扩展功能的目的,这也是目前搜到的关于hook概念提及最多的点)。

因为暂时没有接触到相关的业务模型,所以不是很理解具体业务场景下怎么选择。

Function pointer

函数指针的声明

int (*ptrFunc1) ();		// a pointer named ptrFunc1 that points to a function taking no arguments and returning an integer
void (*ptrFunc2)(int); 	// a pointer named ptrFunc2 that points to a function taking one integer arguments and returning an integer

函数指针的简单示例

#include<stdio.h>
int func(int, int);
int main(void)
{
    int result1,result2;
    /* declaring a pointer to a function which takes
       two int arguments and returns an integer as result */
    int (*ptrFunc)(int,int); 
 
    /* assigning ptrFunc to func's address */                     
    ptrFunc=func; 
 
    /* calling func() through **explicit dereference** */
    result1 = (*ptrFunc)(10,20); 
 
    /* calling func() through **implicit dereference** */         
    result2 = ptrFunc(10,20);               
    printf("result1 = %d result2 = %d\n",result1,result2);
    return 0;
}
 
int func(int x, int y)
{
    return x+y;
}

CALLBACK FUNCTION

Defination

In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer. 1

Example2

// Define callback type
typedef void (*callback)(int);

// Operating function that invokes callback
int MyFunc(char* str,int num, callback curCallback){
	printf("MyFunc has %s\r\n",str);
	curCallback(num);
	return 0;
}

// Callback function defination
void MyCallback(int arg){
	printf("\tbalabala my_callback %d!\r\n", arg);
}

void MyCallback2(int arg){
	printf("\tOhhhhhh! my_callback %d!\r\n", arg);
}

int main(void){
    callback ptrCallback = MyCallback;
	printf("Oh in main!\r\n");
    MyFunc("ptrCallback",6, ptrCallback);
    MyFunc("MyCallback",666, MyCallback);
    MyFunc("&MyCallback",999, &MyCallback);
    MyFunc("MyCallback2",666, MyCallback2);
    return 0;
}

输出:

Oh in main!
MyFunc has ptrCallback
	balabala my_callback 6!
MyFunc has MyCallback
	balabala my_callback 666!
MyFunc has &MyCallback
	balabala my_callback 999!
MyFunc has MyCallback2
	Ohhhhhh! my_callback 666!

用处

在固定接口形式以后,将调用者与被调用者解耦。调用者不需要关心函数的具体实现。
C语言中常用的两个例子:
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
通过将比较函数的具体实现与排序算法解耦,将排序算法的具体实现交给使用者,大大扩展了排序算法的实用性

HOOK FUNCTION

Defination

In computer programming, the term hooking covers a range of techniques used to alter or augment the behaviour of an operating system, applications, or other software components, by intercepting function calls or messages or events passed between software components. Code that handles such intercepted function calls, events or messages is called a hook. 3

Example4

typedef int (*hook)(int,int);
hook g_pFun;	// hook is usually a global variable

int Plat(void) {
    int a = 10;
    int b = 15;
    printf("%d\n", g_pFun(a, b));
    return 0;
}

// registration function
int RegFun(int (*pFun)(int x, int y)) {
    g_pFun = pFun;
    return 0;
}
 
int Max(int x, int y) {
    printf("MAX\r\n");
    return (x > y ? x : y);
}
 
int Min(int x, int y) {
    printf("MIN\r\n");
    return (x < y ? x : y);
}

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    RegFun(Min);
    Plat();
    RegFun(Max);
    Plat();
    return NULL;
}

  1. Wikipedia-Callback ↩︎

  2. Function Pointers and Callbacks in C – An Odyssey ↩︎

  3. WikiPedia-Hooking ↩︎

  4. 优快云-c语言钩子函数 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值