How can one implement an array of functions on C? - c

I am trying to implement an interpreter. I'd love to go with GCC first class labels to make it threaded code, but I should hold on to a standard this time, so naturally I am left with function table. So, I'm doing this:
unsigned short int FUN_TABLE[MAX_FUN] (void*);
And I want to fill it with functions, each getting pointer to its operands, doing its part, returning length of the whole instruction in memory to a dispatcher.
The thing is, I can't even compile it due to the following error: declaration of FUN_TABLE as array of functions. Considering it is exactly what I am trying to achieve, why is this an error, why should I pay it attention, and if I shouldn't, how to suppress it in elegant and standardized manner?

You can define an array of function pointers like this (pseudocode):
int (*funcArr2[10])(param, param, ...) = {NULL};
However, you should be aware that this means that all these functions have the same set of arguments. You can not declare an array with function pointers to totall different functions with regard to their signature.

GCC is telling you: "there is no such thing as an array of functions".
Considering it is exactly what I am trying to achieve, why is this an error, why should I pay it attention
Because you are trying to achieve something that does not exist in the C language. But instead, you can achieve the desired functionality through an array of function pointers.
The syntax of declaring a function pointer is
return_type (*func_ptr_name)(parameters)
and the syntax for declaring an array of function pointers is
return_type (*func_ptr_name[n])(parameters)
Since that syntax is quite obscure, you will not want to use it. The solution is to use typedefs:
typedef unsigned short (*func_table_t)(void*);
// declare an array of function pointers, using readable syntax:
func_table_t func_table [MAX_FUNC] =
{
&some_function,
&some_other_function,
...
};

Arrays of functions aren't legal. Your easiest work around would be an array of pointers to functions -- but this implies that each function being pointed to from the array has the same signature.

Related

How to declare a function if it's return value and parameters are given in command line and unknown before implementation? [duplicate]

I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time.
But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into.
In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that interpret the bytes array like arguments. Since reflection does not exist in C, I have no hope to see this with a function pointer.
One solution that I have in mind (and it's really bad), is to create a model of function pointer at compilation time that will cast in a "hardcoded way" the ptr to the right type to use like this:
void callFunc64Bits(void* funcPtr,long long args);
void callFuncVoid(void* funcPtr);
The problem is that I will have to create like 100 function like this that will cast the pointer correctly.
Is there a way to do it more efficiently?
Thank you very much!
This is a hard problem without, unfortunately, good or easy answers.
See this former SO question: Run-time parameters in gcc (inverse va_args/varargs)
See this C FAQ question: http://c-faq.com/varargs/invvarargs.html
See this collection of "wacky ideas" by the C FAQ list maintainer: http://c-faq.com/varargs/wacky.html
Addendum: see this former SO question: How to call functions by their pointers passing multiple arguments in C?
...which mentions "libffi": http://sourceware.org/libffi/

C++: Is it normal to pass array reference to function

Arrays can be passed as pointer to a function or even as reference. Passing it as reference gives an alias on which sizeof and count operators will also work. This makes pass by reference look superior.
However, pass by pointer seems to be the norm in books. Why? Is there something I particularly need to know about pass by reference for arrays?
Passing by reference means that your function can only accept fixed-size arrays (that's why it knows their size, because the compiler enforces it). Passing by pointer means otherwise. Also, passing by pointer lets you pass nullptr, for better or worse.
I usually use std::vector and like to pass by const reference. That said, if my api may at some point be called by c code, using pass by const pointer may make sense, though you then have to also want to send down the size. If the function may be called with an std::array or a std::vector, you could decide to send down a pointer (and size), or a set of iterators (begin/end).
If we are talking about using std::array, the template argument requires the size of the array. This would mean in a normal function, you'd need a fixed size:
void myfunc( const std::array<int, 5>& mydata ){...}
However, if we do a templated function, templating on size, that is no longer a problem.
template<unsigned int SZ>
void myfunc(const std::array<int, SZ>& mydata) {...}
If we are talking about stack allocated c-style arrays... Good C++ style is to prefer std::array/std::vector to c-style arrays. I would recommend reading C++ Coding Standard by Herb Sutter chapter 77 on page 152 speaks about the subject. When using c-style arrays, sending down the pointer and size is the standard way to go.

C late binding with unknown arguments

I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time.
But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into.
In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that interpret the bytes array like arguments. Since reflection does not exist in C, I have no hope to see this with a function pointer.
One solution that I have in mind (and it's really bad), is to create a model of function pointer at compilation time that will cast in a "hardcoded way" the ptr to the right type to use like this:
void callFunc64Bits(void* funcPtr,long long args);
void callFuncVoid(void* funcPtr);
The problem is that I will have to create like 100 function like this that will cast the pointer correctly.
Is there a way to do it more efficiently?
Thank you very much!
This is a hard problem without, unfortunately, good or easy answers.
See this former SO question: Run-time parameters in gcc (inverse va_args/varargs)
See this C FAQ question: http://c-faq.com/varargs/invvarargs.html
See this collection of "wacky ideas" by the C FAQ list maintainer: http://c-faq.com/varargs/wacky.html
Addendum: see this former SO question: How to call functions by their pointers passing multiple arguments in C?
...which mentions "libffi": http://sourceware.org/libffi/

Template in ansi C?

How I can create function with other types of data (some struct or sth)? In C++ exist templates, but in C?
I hear about void *, but i dont know if it works.
Any ideas?
Well, the way to do it is with void *. You might also need use function pointers, for example if you need to compare generic values.
The other way to do it is to use xmacros, but that's generally more for reducing code duplication for very similar structures.
void * is the solution in C as any pointer has the same sizeof() as void *. Of course, you get no type safety, but it's as good an abstraction as you can get with C. Furthermore, you could look at stdarg.h and variadic functions, but again, you should keep track yourself of what you're doing, since the compiler won't aid you one bit.

How to best fix both warnings(old style c-function declaration isn't a prototype)

I was fixing some functions in a piece of someone else code that included a number of functions that took no arguments.
They were declared as
return_type_t func();
instead of
return_type_t func(void);
Then I found that a bunch of these were put in a array of structs with function pointers.
When I fixed the function pointer to take void it gave me another warning since 1 of the function took a char* instead of void ptr.
What's the best solution for this sort of thing without a large rewrite(as the code is complex I was mainly cleaning it up around the edges and wish to avoid changing how it flows)?
The struct definition needs to list the correct function type in the member, there is no way around that if you want type safety.
In C, the old-style declaration return_type_t func() doesn't mean that func takes no arguments, it means that the number and types of its arguments (if any) are not specified. You cannot just assume that you can add void inside the parentheses.
Without seeing more of your code, my advice is to investigate each function separately and find out the correct number and types of parameters for each, and then fill out the prototype accordingly.
You can rewrite all your prototype to take a char* and then cast to (void) parameter where the parameter is not usefull

Resources