473,770 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arbitrary Function Arrays

Hi,

We know we can build arrays of variables of the same type and arrays
of functions of the same "type" (i.e., same return value and same
parameters), but is there a way to automate the calling of a sequence
of functions with arbitrary return types and/or parameters?
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
ra*********@son yericsson.com, 919-472-1124
Nov 14 '05 #1
3 1826
Randy Yates wrote:
Hi,

We know we can build arrays of variables of the same type and arrays
of functions of the same "type" (i.e., same return value and same
parameters), but is there a way to automate the calling of a sequence
of functions with arbitrary return types and/or parameters?


Functions cannot be array elements: they are not data
objects, and they do not have size. You are probably thinking
of an array of pointers to functions; pointers are data objects,
have size, and can be elements in an array.

Next, all the elements in an array of function pointers
must have the same type. This is the same as for any other
array: you can't have an array whose [0] element is an `int',
whose [1] element is a `double', and whose [2] element is a
`const struct muggle_descript or*'. The pointers may, however,
have been converted from disparate types to a common type by
casting -- of course, they've got to be converted back to match
the actual called function at the point of the call.

However, the function call construct in C is "static" in
the sense that the type of the return value and the number and
types of the arguments are fixed at compile time. Even for a
variadic function, which can be called with differing argument
lists at different points in the program, the circumstances of
any particular call in the source are unchangeable. When you
write `foo(x,y)' the call passes exactly two arguments of
exactly the same type, every time you execute it. There's no
way to get rid of an argument, add an argument, or change the
type of an argument other than by editing and recompiling.

I've encountered two approaches to working around this
inflexibility. One is to enumerate all the function types
of interest, and to use a big `switch' or something of the
kind to choose between the appropriate calls:

fptr = ...;
switch (ftype) {
case INT_VOID:
intres = ((int(*)(void)) f)();
break;
case DBL_INT:
dblres = ((double(*)(int ))f)(42);
break;
...

This becomes messy unless the number of different function
signatures is quite small.

A second approach is to use "wrapper" functions. You use
an array of `void*' or an array of unions or some such to hold
the actual arguments -- you can build the array at run-time --
and pass it to the wrapper. The wrapper plucks the appropriate
arguments from the array, passes them to the target function,
and returns the result. As a variation, you can use one wrapper
for each different target function signature, passing a pointer
to the target function along with the array of "anonymous"
arguments.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #2
On 11 Mar 2005 08:14:44 -0500,
Randy Yates <ra*********@so nyericsson.com> wrote
in Msg. <xx************ *@usrts005.corp users.net>
We know we can build arrays of variables of the same type and arrays
of functions of the same "type" (i.e., same return value and same
parameters), but is there a way to automate the calling of a sequence
of functions with arbitrary return types and/or parameters?


To the actual question, see Eric's exhaustive posting. I'd just like to
ask: If it was possible to construct an array of pointers to functions
with different signatures, what good would it do you? I'm just curious
about what you're trying to accomplish here.

--Daniel
Nov 14 '05 #3
On Fri, 11 Mar 2005 08:14:44 -0500, Randy Yates wrote:
We know we can build arrays of variables of the same type and arrays of
functions of the same "type" (i.e., same return value and same
parameters), but is there a way to automate the calling of a sequence of
functions with arbitrary return types and/or parameters?


C doesn't let you overload function definitions. Maybe something
like this is what you're looking for:

typedef struct arbfunc_s {
union {
int (*i)(.....);
char * (*c)(.....);
} u;
int kind;
} arbfunc_t;

arbfunct_t actions[ 100 ] = {
{ intfunc, IS_INT },
{ anotherIntFunc, IS_INT },
{ mychar, IS_CHAR }
{ 0 }
};

....
arbfunc_t * ap = actions;
....
for( ap = actions; ap->kind > 0; ++ap ) {
switch( ap->kind ) {
case IS_INT: results = (ap->u.i)(....); break;
case IS_CHAR: results = (ap->u.c)(...); break;
}
}

depending on what you're doing, this may work.

Cheers

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1485
by: Garry Hodgson | last post by:
i'm building a test suite on top of unittest, and some of the tests involve things that might hang, like trying to connect to a wedged server. so i'd like a simple function that i can call that will run a given (func,args) pair and return either the value or raise an exception if it times out. this seems like it should be straightforward, but i've not had much luck getting it to work. my latest attempt, below, raises the exception ok,...
58
10181
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
5
2963
by: Honestmath | last post by:
Hello everyone, I'm using a root finding algorithm in a function that only takes as arguments a function pointer and two variables that represent guesses at the roots of the function. int zbrac(float (*func)(float), float *x1, float *x2) { float f1,f2; f1=(*func)(*x1);
11
4469
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
5
1452
by: Steven D'Aprano | last post by:
I have a problem and I don't know where to start looking for a solution. I have a class that needs to call an arbitrary function and wait for a result. The function, being completely arbitrary and not under my control, may be very time consuming and possibly may not even halt. My class needs to be able to give up waiting for a result after a specified amount of time. I'm thinking something conceptually like this:
11
407
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
2
2468
by: darknails | last post by:
Hi, I would like to know how to define a arbitrary mathematical function with one variable and a list of paramter, like, f(x). For example, in GSL library this is done as /* Definition of an arbitrary function with parameters */ struct gsl_function_struct { double (* function) (double x, void * params);
6
1529
by: Adam | last post by:
Hey, Just a quick appeal for suggestions re some code, able to pass varying numbers of arguments... I have a function which can accept varying numbers of arguments. It in turn calls a function whose argument list can vary in length. At the moment, I have something along the lines of...
18
2877
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything about f, except how to call it.
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.