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. 8 1667
In article <sl*************@www.odyniec.pl>, <a@aaa.aaa.aaawrote:
>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
<a@aaa.aaa.aaawrote in message news:sl*************@www.odyniec.pl...
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 ?
Do you mean something like this?
#include <stdio.h>
float f0(float x){ return x*0;}
float f1(float x){ return x*1;}
float f2(float x){ return x*2;}
float (*fntable[3])(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
Hello again and sorry for the different nicknames. I'm still
struggling with my reader.
On 2008-04-13, Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>
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 <bc@freeuk.comwrote:
>
Do you mean something like this?
#include <stdio.h>
float f0(float x){ return x*0;}
float f1(float x){ return x*1;}
float f2(float x){ return x*2;}
float (*fntable[3])(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.
On Sun, 13 Apr 2008 15:25:20 +0000 (UTC), a@aaa.aaa.aaa wrote:
>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) ?
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[0] = f0;
func_array[1] = f1; ...
Obviously, the functions must be declared previously and must be
defined prior to linking.
Remove del for email
John Doe <jo**@doe.comwrites:
On 2008-04-13, Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>> 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 <bc@freeuk.comwrote:
>> Do you mean something like this?
#include <stdio.h>
float f0(float x){ return x*0;} float f1(float x){ return x*1;} float f2(float x){ return x*2;}
float (*fntable[3])(float) = {&f0, &f1, &f2};
<snip>
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.
On Apr 13, 7:10*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
John Doe <j...@doe.comwrites:
On 2008-04-13, Walter Roberson <rober...@ibd.nrc-cnrc.gc.cawrote:
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 <b...@freeuk.comwrote:
Do you mean something like this?
#include <stdio.h>
float f0(float x){ return x*0;}
float f1(float x){ return x*1;}
float f2(float x){ return x*2;}
float (*fntable[3])(float) = {&f0, &f1, &f2};
<snip>
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
"John Doe" <jo**@doe.comwrote in message news:ft**********@news.onet.pl...
Hello again and sorry for the different nicknames. I'm still
struggling with my reader.
On 2008-04-13, Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>> 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 <bc@freeuk.comwrote:
>> Do you mean something like this?
#include <stdio.h>
float f0(float x){ return x*0;} float f1(float x){ return x*1;} float f2(float x){ return x*2;}
float (*fntable[3])(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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Simon Schaap |
last post by:
Hello,
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a
function that will split it up (on every location where...
|
by: Peter B. Steiger |
last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C
is a simple word game that involves stringing together random lists of
words. In the Pascal version the whole array was...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: ebc |
last post by:
Hi,
I have written a function that removes double entries from a sorted
array.
See the structures
typedef struct tagRECR
{
char name;
|
by: intrepid_dw |
last post by:
Hello, all.
I've created a C# dll that contains, among other things, two functions
dealing with byte arrays. The first is a function that returns a byte
array, and the other is intended to...
|
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is...
|
by: StevenChiasson |
last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller.
This is more or less, your...
|
by: Adam Chapman |
last post by:
Hi,
Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I've
realised that C can't determine the size of a 2d...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |