Afficher un message
Vieux 14/04/2008, 01h48   #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
 
Page generated in 0,07182 seconds with 9 queries