1

I know the reasons why this is a bad idea and I also know that this is the reason C++ templates were created, but I'm doing it in C for the fun and learning.

I'm embedding Python into my application and I wanted to make it possible to register certain C functions, with any arbitrary type, to be callable at the application run-time. Because of this a pointer to them is stored (as a simple void*).

With some basic macro fun I have gotten all the information I need about these functions and stored that too - the function pointer, the size of the return value (and if it is void), the number of arguments and each of their sizes.

So I'm stuck at the final stage - which is the actual calling of the function pointer with the appropriate data. I'm fairly certain this sort of thing should be possible (I've caused stack errors in my own time), but I was wondering if there was a more...legitimate way to do it.

I guess the ideal would look something like this:

void call_function(void* function, void* return, void* arg_data, int arg_data_size);

Does such a thing exists?

Thanks

3
  • 1
    There's definitely no platform-independent mechanism for this. The closest you can get is variadic functions. Commented Apr 9, 2012 at 20:30
  • You can do it, but it'll be machine dependent. Look into C calling convention(s) on your choice of OS, compiler, and architecture. (E.g., cdecl for x86 gcc on unix, sysv-amd64 for amd64 unix, stdcall for win32 x86…) Commented Apr 9, 2012 at 22:51
  • @ConradMeyer Thanks! Taking a deeper look now, even the ASM is going to be complicated. Perhaps I've bitten off more than I can chew here... Commented Apr 10, 2012 at 15:43

1 Answer 1

1

You can declare a function pointer void* (*f) (void*); that points to a function that takes a void* argument and returns a void* return value -- you can put this in the first parameter of call_function.

Then call call_function as:

void* function(void*);
ret_type ret;
arg_type arg_data;
call_function(&function, (void*)&ret, (void*)&arg_data, sizeof(arg_data));

where arg_type is the actual argument type you want to use inside function and ret_type is the actual type of the return value of function.

Note: you might want to specify the size of the return value type as well.

Note: this will work with any function of one argument. The solution can be extended to a fixed/known number of arguments in function, but not to handle an unknown number of arguments.

Note: naming the second parameter return is not allowed as return is a keyword.

Sign up to request clarification or add additional context in comments.

6 Comments

This also doesn't deal with multiple input arguments.
I'm not really sure I get what you are saying. My imaginary function "call_function" doesn't actually exist. Otherwise I would be already done.
@OliCharlesworth - it is not clear from the question for me that the OP wanted to handle arbitrary number of arguments
@Attila: The question does say "the number of arguments and each of their sizes".
You were asking (as I understood), whether you can declare/write a function that can take a function and a parameter of arbitrary type/return value and call that function with the specified parameter. My answer is how you would declare such a function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.