0

In C, From What is guaranteed about the size of a function pointer? it's seems like generic generic function pointers are possible.

Suppose you store the function signature along with the pointer (in a way or another... for example : the size of each parameters), Is it possible to call dynamically this function ? (the signature is only known at runtime). The return type is void for all the function, and only the parameter list changes

I was wondering if casting to a varargs function would do the trick ?

(Bonus) The question is for C, but in C++, is it possible to use a similar techniques, and to apply it on instance methods ?

(I don't expect necessarily standard-compliant solution, but still something that is supported enough by most compilers)

hl037_
  • 3,520
  • 1
  • 27
  • 58
  • Don't ask for both C and C++ in a single question – Support Ukraine Aug 26 '21 at 10:21
  • @TomKarzes yes, I'll edit – hl037_ Aug 26 '21 at 10:22
  • 1
    Does this answer your question? [Is there such a thing as a generic function pointer in C that can be assigned/cast to a more restrictive prototype?](https://stackoverflow.com/questions/31482624/is-there-such-a-thing-as-a-generic-function-pointer-in-c-that-can-be-assigned-ca) – Tom Karzes Aug 26 '21 at 10:23
  • Can you give us some examples of functions? Do they have the same return type? Are only the argument list different? – mch Aug 26 '21 at 10:28
  • Oh forgot about the return type... it's void, I precise. Only the arg list is different – hl037_ Aug 26 '21 at 10:28
  • @mch no, the question is about actually making the call (from a `void (*)(void)` pointer) – hl037_ Aug 26 '21 at 10:30
  • 2
    "store the function signature along with the pointer" You cannot (as far as the standard goes). "in a way or another... for example : the size of each parameters" This is not enough. Nothing is enough. You cannot store a signature as data and then interpret that data to make an actual call. – n. m. could be an AI Aug 26 '21 at 10:36
  • 1
    As for non-standard-compliant solutions, try [libffi](https://sourceware.org/libffi/). – n. m. could be an AI Aug 26 '21 at 10:39
  • 2
    See [C late binding with unknown arguments](https://stackoverflow.com/questions/34885868). See also [How to push n arguments to stack without assembler](https://stackoverflow.com/questions/68184297). – Steve Summit Aug 26 '21 at 11:19

1 Answers1

0

You can do that:

#include <stdio.h>

void foo(int a)
{
    printf("foo\n");
}

void bar(char* a, double b)
{
    printf("bar\n");
}

void baz(void)
{
    printf("baz\n");
}

int main()
{
    void (*arr[])() = {foo, bar, baz};

    arr[0](1, 1, 2);
    arr[1]("test", 2.0);
    arr[2]();
}

An empty parameter list in a function pointer means unspecified number of parameters. But you have to remember the parameters, when you call a function. Additional parameters are ignored (see arr[0](1, 1, 2);). Too few parameters is a problem.

mch
  • 9,424
  • 2
  • 28
  • 42
  • This won't work reliably. `void (*arr[])()` is an array of pointers to functions that take an indeterminate number of arguments. The arguments to functions like that undergo [default argument promotions](https://port70.net/~nsz/c/c11/n1570.html#6.5.2.2p6), which functions such as `void foo(int a)` **do not expect**. This may **appear** to work, but it's wrong. – Andrew Henle Aug 29 '21 at 16:21