简体   繁体   English

C:将 function 指针从通用输入 arg (void*) 类型转换为特定 (char*/int *) arg 和/或不同的返回类型,void,int

[英]C: typecasting function pointers from generic input arg (void*) into specific (char*/int *) arg and/or different return types, void, int

I have specific data types:我有特定的数据类型:

typedef struct _A_s { ... } A_t;
typedef struct _B_s { ... } B_t;

I also have specific functions to operate on these (eg A_t * init_A(); free_A(A_t *); print(A_t *); etc.我也有特定的功能来操作这些(例如A_t * init_A(); free_A(A_t *); print(A_t *);等等。

A 3rd function takes in any of those data struct types via a void * and also function pointers in order to free and print its input.第三个 function 通过void *和 function 指针接收任何这些数据结构类型,以便释放和打印其输入。 I want to keep this function generic, hence converting A_t* to void* .我想保留这个 function 的通用性,因此将A_t*转换为void* But I am not sure how that will work with function pointers.但我不确定这将如何与 function 指针一起使用。

Here is a rough (sorry:) sketch:这是一个粗略的(抱歉:)草图:

typedef (void )(*free_generic_t)(void *);
typedef (void )(*print_generic_t)(void *);
void myfunc(
  void *object,
  free_generic_t free_fn,
  print_generic_t print_fn
){
  ...
  print_fn(object);
  free_fn(object);
}

My aim is to do this:我的目标是这样做:

A_t *a = init_A(...);
B_t *b = init_B(...);
myfunc((void *)a, free_A, print_A);
myfunc((void *)b, free_B, print_B);

but gcc complains because of -Werror=incompatible-pointer-types , which I want to keep on.但是gcc抱怨是因为-Werror=incompatible-pointer-types ,我想继续这样做。 Ignoring this warning, the program works fine.忽略此警告,程序运行正常。

When I did this typecast当我做这个类型转换时I got a SIGSEGV我有一个 SIGSEGV it worked fine and passed the valgrind test:它运行良好并通过了valgrind测试:

myfunc(
  (void *)a,
  (free_generic_t )free_A,
  (free_generic_t )print_A
);

Question 1: How far can I go in typecasting functions to function pointers in the above scenario?问题 1:在上述场景中,我 go 可以将函数类型转换为 function 指针多远? Assume that the number of input parameters is fixed.假设输入参数的个数是固定的。 That is, all print_A() 's take just 1 argument in, which will be a pointer.也就是说,所有print_A()只接受 1 个参数,这将是一个指针。

Question 2: The problem becomes more complex when some of those specific functions return int and some nothing ( void ).问题 2:当某些特定函数return int而某些函数返回空值 ( void ) 时,问题变得更加复杂。 Can I adjust the generic function pointers to return int in order to accommodate both flavours of specific functions even if some of them return void ?我可以调整通用 function 指针以return int以适应两种特定函数的风格,即使其中一些返回void吗?

Update : I don't get a SIGSEGV when I typecast the function pointers, it was a mistake of mine.更新:当我输入 function 指针时,我没有得到 SIGSEGV,这是我的错误。 I have edited my post in place.我已经就地编辑了我的帖子。

Give those structs给那些结构

typedef struct _A_s { ... } A_t;
typedef struct _B_s { ... } B_t;

an pointer to a deallocator function and populate it in the A_t * init_A();指向释放器 function 的指针并将其填充到A_t * init_A(); respectively B_t * init_B();分别为B_t * init_B(); . .

Then you may write a generic deallocator for both of them which indirectly calls the deallocator via the stored pointer.然后你可以为它们编写一个通用的释放器,它通过存储的指针间接调用释放器。

// Anything, which is shared between structures
typedef struct _X_s { void (*deallocator)(void *); ... } X_t;

typedef struct _A_s { X_t x; ... } A_t;
typedef struct _B_s { X_t x; ... } B_t;

Then然后

void deallocate(X_t *x){
    x -> deallocator(x);
}

int main(void){
    A_t *a = init_A();
    B_t *b = init_B();

    deallocate((X_t *)a);
    deallocate((X_t *)b);
}

Please be aware that this is only a concept of how you can do it.请注意,这只是您如何做到这一点的概念。 You must work out the details by yourself and add validity checks and such.您必须自己制定详细信息并添加有效性检查等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM