435,572 Members | 2,949 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,572 IT Pros & Developers. It's quick & easy.

An array of function pointers - generated parametrically

 P: n/a Hello. Suppose I have a family of functions f_0(x) = 0*x f_1(x) = 1*x .... f_n(x) = n*x taking float and returning float (naturally, the actual functions would be much more complex). I'd like to pass the pointers to these functions one by one (in a loop) to another function accepting an argument of type float (*)(float). The problem is that the amount of these functions (or the n parameter) will be determined at the runtime. Is there a way to generate somehow such a family in the form of an array float (*[])(float) ? Or, having a function float func(int i, float x) { return i*x; } is it possible to "cast" it somehow to obtain an array indexed by i such that (symbolically) a[i](x) = func(i, x) for all n and x ? Thanks. Jun 27 '08 #1
8 Replies

 P: n/a In article , Suppose I have a family of functions >The problem is that the amount of these functions (or the n parameter)will be determined at the runtime. There is no way to generate functions at run-time in C. There is also no way to create an "instance" of a function that has some stored value that is different than a different "instance" of the same function. Or to phrase it in terms that some other languages use, there are no "closures" in standard C. -- "Whenever there is a hard job to be done I assign it to a lazy man; he is sure to find an easy way of doing it." -- Walter Chrysler Jun 27 '08 #2

 P: n/a float f0(float x){ return x*0;} float f1(float x){ return x*1;} float f2(float x){ return x*2;} float (*fntable)(float) = {&f0, &f1, &f2}; int main(void) {int i; float x,y; for (i=0; i<3; ++i) { x = i*10.0; y = fntable[i](x); printf("%d : f%d(%f) = %f\n",i,i,x,y); } } -- Bart Jun 27 '08 #3

 P: n/a Hello again and sorry for the different nicknames. I'm still struggling with my reader. On 2008-04-13, Walter Roberson There is no way to generate functions at run-time in C. There is also no way to create an "instance" of a function that has some stored value that is different than a different "instance" of the same function. Or to phrase it in terms that some other languages use, there are no "closures" in standard C. That's a pity, but really: not a tragedy. There was an easy workaround in this case. On 2008-04-13, Bartc Do you mean something like this? #include float f0(float x){ return x*0;} float f1(float x){ return x*1;} float f2(float x){ return x*2;} float (*fntable)(float) = {&f0, &f1, &f2}; int main(void) {int i; float x,y; for (i=0; i<3; ++i) { x = i*10.0; y = fntable[i](x); printf("%d : f%d(%f) = %f\n",i,i,x,y); } } No, what I meant was: 1. The value of n is determined at the runtime. 2. Space for an array of function pointers is allocated. 3. Now some magic is done and these pointers point to f_0, ... f_n. The point is, as I suppose, there is no way to dynamically allocate space for a function because its code size is unpredictable. Thanks for the replies. Jun 27 '08 #4

 P: n/a On Sun, 13 Apr 2008 15:25:20 +0000 (UTC), a@aaa.aaa.aaa wrote: >Hello.Suppose I have a family of functionsf_0(x) = 0*xf_1(x) = 1*x...f_n(x) = n*xtaking float and returning float (naturally, the actual functions wouldbe much more complex).I'd like to pass the pointers to these functions one by one (in a loop)to another function accepting an argument of type float (*)(float).The problem is that the amount of these functions (or the n parameter)will be determined at the runtime.Is there a way to generate somehow such a family in the form of an arrayfloat (*[])(float) ? To simplify the discussion, let's define a typedef alias for the pointer in question. typedef float (*func_ptr)(float); If you have a C99 system, you can define a variable length array whose size n is dynamically determined at run time. func_ptr func_array[n]; If you don't have C99 (or even if you do), you can allocate space for such an array. func_ptr *func_array; func_array = malloc(n * sizeof *func_array); In both cases, you need to assign values to the array elements. func_array = f0; func_array = f1; ... Obviously, the functions must be declared previously and must be defined prior to linking. Remove del for email Jun 27 '08 #5

 P: n/a John Doe >There is no way to generate functions at run-time in C. There isalso no way to create an "instance" of a function that has somestored value that is different than a different "instance" of thesame function. Or to phrase it in terms that some other languagesuse, there are no "closures" in standard C. That's a pity, but really: not a tragedy. There was an easy workaround in this case. On 2008-04-13, Bartc >Do you mean something like this?#include float f0(float x){ return x*0;}float f1(float x){ return x*1;}float f2(float x){ return x*2;}float (*fntable)(float) = {&f0, &f1, &f2}; No, what I meant was: 1. The value of n is determined at the runtime. 2. Space for an array of function pointers is allocated. The above can be altered so that the array is allocated with a run time size. However... 3. Now some magic is done and these pointers point to f_0, ... f_n. Walter Robertson's comment is important. Since functions can't be "made" at run time, there is no point altering the above to make the size a run-time expression because there can only be a fixed number of functions in your program. Every function must be there, written out, for the compiler to compile. The pattern you want is *very* common in many programming languages, but C can't do it. If you really want to do this, you need to look at another language. There are two get-out clauses: (1) you can use dynamic linking to "pull in" functions at run time, but this is not portable and the functions are still not run-time values. (2) If the functions share a lot in common (for example if they are all polynomials in some fixed set of variables) then you can write one single simulator function and pass it data (in this case, the coefficients). In effect, you swap a set of function pointers for one function and a set of data pointers. The point is, as I suppose, there is no way to dynamically allocate space for a function because its code size is unpredictable. You are probably saying the same thing as I am in another way. I hope my way of putting it helps a bit. -- Ben. Jun 27 '08 #6

 P: n/a On Apr 13, 7:10*pm, Ben Bacarisse float f0(float x){ return x*0;} float f1(float x){ return x*1;} float f2(float x){ return x*2;} float (*fntable)(float) = {&f0, &f1, &f2}; No, what I meant was: 1. The value of n is determined at the runtime. 2. Space for an array of function pointers is allocated. The above can be altered so that the array is allocated with a run time size. *However... 3. Now some magic is done and these pointers point to f_0, ... f_n. Walter Robertson's comment is important. *Since functions can't be "made" at run time, there is no point altering the above to make the size a run-time expression because there can only be a fixed number of functions in your program. *Every function must be there, written out, for the compiler to compile. The pattern you want is *very* common in many programming languages, but C can't do it. *If you really want to do this, you need to look at another language. There are two get-out clauses: (1) you can use dynamic linking to "pull in" functions at run time, but this is not portable and the functions are still not run-time values. *(2) If the functions share a lot in common (for example if they are all polynomials in some fixed set of variables) then you can write one single simulator function and pass it data (in this case, the coefficients). *In effect, you swap a set of function pointers for one function and a set of data pointers. The point is, as I suppose, there is no way to dynamically allocate space for a function because its code size is unpredictable. You are probably saying the same thing as I am in another way. *I hope my way of putting it helps a bit. -- Ben.- Hide quoted text - - Show quoted text - Hi List, Hello John I really can't se why your same function can't accept values on runtime. But you can always use some embedded language like lua (www.lua.org) to handle this, loading new scripts on demand (building new scripts from your application maybe...). Regards Rafael Jun 27 '08 #7

 P: n/a "John Doe" >There is no way to generate functions at run-time in C. There isalso no way to create an "instance" of a function that has somestored value that is different than a different "instance" of thesame function. Or to phrase it in terms that some other languagesuse, there are no "closures" in standard C. That's a pity, but really: not a tragedy. There was an easy workaround in this case. On 2008-04-13, Bartc >Do you mean something like this?#include float f0(float x){ return x*0;}float f1(float x){ return x*1;}float f2(float x){ return x*2;}float (*fntable)(float) = {&f0, &f1, &f2};int main(void){int i;float x,y;for (i=0; i<3; ++i){ x = i*10.0; y = fntable[i](x); printf("%d : f%d(%f) = %f\n",i,i,x,y);}} No, what I meant was: 1. The value of n is determined at the runtime. 2. Space for an array of function pointers is allocated. 3. Now some magic is done and these pointers point to f_0, ... f_n. The point is, as I suppose, there is no way to dynamically allocate space for a function because its code size is unpredictable. It's still not clear whether the code for these functions already exists somewhere at compile-time. Functions created at runtime aren't easy in C as has been pointed out. But, although the code won't be as simple as you've indicated, could there be any way of programmatically calculating what function n does? So for your example, f_n(x) is simply x*n; you don't need an array of functions for that. So is there any similarity between functions that can be exploited? If you give a few examples then we can see what's possible. -- Bart Jun 27 '08 #8

 P: n/a a@aaa.aaa.aaa wrote: ... is it possible to "cast" it somehow to obtain an array indexed by i such that (symbolically) a[i](x) = func(i, x) for all n and x ? ... No, you can't do it in C literally the way you describe it. When it is necessary to implement something like that in C, you'd normally stick with one generic function (or fixed number of generic functions with unified interface), parametrized to the point when it's capable of covering all required behaviors of the family. Then you'd store the parameter sets in your array, and later retrieve and pass the correct set of parameters when calling the generic function. This will be essentially a "manual" implementation of C++ classes (with or without virtual functions) with your original functions getting generalized into "functors". It is not as elegant as the "closure" functionality you describe in your message, of course. And if the calling code strictly requires function pointers without letting you pass some user context for the call, then you are out of luck. -- Best regards, Andrey Tarasevich Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion. 