PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > An array of function pointers - generated parametrically
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
An array of function pointers - generated parametrically

Réponse
 
LinkBack Outils de la discussion
Vieux 13/04/2008, 16h25   #1
a@aaa.aaa.aaa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut An array of function pointers - generated parametrically

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.
  Réponse avec citation
Vieux 13/04/2008, 16h35   #2
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An array of function pointers - generated parametrically

In article <slrng049cd.rq.a@www.odyniec.pl>, <a@aaa.aaa.aaa> wrote:

>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
  Réponse avec citation
Vieux 13/04/2008, 17h07   #3
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An array of function pointers - generated parametrically


<a@aaa.aaa.aaa> wrote in message news:slrng049cd.rq.a@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


  Réponse avec citation
Vieux 13/04/2008, 22h36   #4
John Doe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An array of function pointers - generated parametrically

Hello again and sorry for the different nicknames. I'm still
struggling with my reader.

On 2008-04-13, Walter Roberson <roberson@ibd.nrc-cnrc.gc.ca> wrote:
>
> 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.com> wrote:
>
> 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.



  Réponse avec citation
Vieux 14/04/2008, 00h03   #5
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An array of function pointers - generated parametrically

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
  Réponse avec citation
Vieux 14/04/2008, 00h10   #6
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An array of function pointers - generated parametrically

John Doe <john@doe.com> writes:
> On 2008-04-13, Walter Roberson <roberson@ibd.nrc-cnrc.gc.ca> wrote:
>>
>> 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.com> wrote:
>>
>> 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 s a bit.

--
Ben.
  Réponse avec citation
Vieux 14/04/2008, 00h48   #7
soscpd@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An array of function pointers - generated parametrically

On Apr 13, 7:10pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> John Doe <j...@doe.com> writes:
> > On 2008-04-13, Walter Roberson <rober...@ibd.nrc-cnrc.gc.ca> wrote:

>
> >> 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.com> wrote:

>
> >> 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 s 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
  Réponse avec citation
Vieux 14/04/2008, 00h50   #8
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An array of function pointers - generated parametrically


"John Doe" <john@doe.com> wrote in message news:fttucn$13h$1@news.onet.pl...
> Hello again and sorry for the different nicknames. I'm still
> struggling with my reader.
>
> On 2008-04-13, Walter Roberson <roberson@ibd.nrc-cnrc.gc.ca> wrote:
>>
>> 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.com> wrote:
>>
>> 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



  Réponse avec citation
Vieux 14/04/2008, 15h53   #9
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An array of function pointers - generated parametrically

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h46.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,26045 seconds with 17 queries