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 1739
In article <sl************ *@www.odyniec.p l>, <a@aaa.aaa.aaaw rote:
>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.aaaw rote in message news:sl******** *****@www.odyni ec.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.n rc-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.comw rote:
>
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)(flo at);
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.comwr ites:
On 2008-04-13, Walter Roberson <ro******@ibd.n rc-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.comw rote:
>> 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.comwr ites:
On 2008-04-13, Walter Roberson <rober...@ibd.n rc-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.co mwrote:
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.comwr ote 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.n rc-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.comw rote:
>> 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 programmaticall y 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 code...
TCHAR myArray;
DoStuff(myArray);
|
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 a * occurs in
the string). This split function should allocate a 2D array of chars
and put the split results in different rows. The listing below shows
how I started to work on this. To keep the program simple and help
focus the program the...
|
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 static; if the input
file contained more than entries, tough.
This time I want to do it right - use a dynamic array that increases in
size with each word read from the file. A few test programs that make use
of **List and realloc( List, blah...
|
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 = {0,1,2,4,9};
|
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 receive a byte array as one of its
parameters. The project is marked for COM interop, and that all
proceeds normally.
When I reference the type library in the VB6 project, and write the
code to call the function that returns the byte array, it works
|
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 passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont...
|
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 standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress. This function returns an array of pointers that are pointing to created objects. In action, all it...
|
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 array, so im
inputting the dimensions of those myself.
The problem is that the output array (C=A*B) has as many rows as A and
as many columns as B. I would think of initialising C with:
|
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...
|
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,...
| |
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |