473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

create function at runtime

hi friends , how can I declare / create function during runtime similiar to
lambda in lisp.

thanks

Mohan
Nov 14 '05 #1
13 4357
"invincible" <mo**********@in.bosch.com> wrote:
hi friends , how can I declare / create function during runtime similiar to
lambda in lisp.


You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
aware of without resorting to the most hairy of hacks. It would probably
involve writing machine code directly, and you really don't want to do
that unless you absolutely must. Note that "absolutely must" includes
"cannot possibly use a language which is better suited for this; at a
pinch, even using Clipper will be better than munging your own code
space."

Richard
Nov 14 '05 #2
Well there is no facility in C language for this. Well if you want to
create something like this you can try ony way that is giving a feel
that actually you are creating a function at run time but in actual you
dont...You take input and parse the input yourself and the if a
statement like this is there::
printf("hello world")
then make it str="hello World";
and puts(str);

Nov 14 '05 #3
"DeltaOne" <sh**********@wipro.com> writes:
Well there is no facility in C language for this. Well if you want to
create something like this you can try ony way that is giving a feel
that actually you are creating a function at run time but in actual you
dont...You take input and parse the input yourself and the if a
statement like this is there::
printf("hello world")
then make it str="hello World";
and puts(str);

<OT>
.... or your process writes the C text to a file, spawns other
processes to compile and link the C, then dynamically load the resulting
shared object into the running process, if your operating system
supports it.
</OT>

--
Chris.
Nov 14 '05 #4
"invincible" <mo**********@in.bosch.com> writes:
hi friends , how can I declare / create function during runtime similiar to
lambda in lisp.


Probably by implementing a Lisp interpreter in C.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Keith Thompson <ks***@mib.org> writes:
"invincible" <mo**********@in.bosch.com> writes:
hi friends , how can I declare / create function during runtime similiar to
lambda in lisp.
Probably by implementing a Lisp interpreter in C.


or a C interpreter in C:

http://www.softintegration.com/products/chstandard/

--
Chris.
Nov 14 '05 #6
On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"invincible" <mo**********@in.bosch.com> wrote:
hi friends , how can I declare / create function during runtime similiar to
lambda in lisp.


You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
aware of without resorting to the most hairy of hacks.


You could write an interpreter, and your C programme could write the
fn out to file / into memory, invoke the interpreter on it, and
process the results.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #7
Chris McDonald wrote:
"DeltaOne" <sh**********@wipro.com> writes:
Well there is no facility in C language for this. Well if you want to
create something like this you can try ony way that is giving a feel
that actually you are creating a function at run time but in actual you
dont...You take input and parse the input yourself and the if a
statement like this is there::
printf("hello world")
then make it str="hello World";
and puts(str);


<OT>
... or your process writes the C text to a file, spawns other
processes to compile and link the C, then dynamically load the resulting
shared object into the running process, if your operating system
supports it.
</OT>


Or you embed a C interpreter written in standard C in your program.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #8
Mark McIntyre <ma**********@spamcop.net> wrote:
On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"invincible" <mo**********@in.bosch.com> wrote:
hi friends , how can I declare / create function during runtime similiar to
lambda in lisp.


You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
aware of without resorting to the most hairy of hacks.


You could write an interpreter, and your C programme could write the
fn out to file / into memory, invoke the interpreter on it, and
process the results.


Sure, but that's a whole different thing. Then you're creating _another_
programming environment within the larger program, the language of which
may be deceptively similar to the one your main program is written in.
AFAIAA all lambda functions in all Lisp-alikes create new functions as a
part of the main program itself.

Richard
Nov 14 '05 #9
Chris McDonald wrote:
Keith Thompson <ks***@mib.org> writes:

"invincible" <mo**********@in.bosch.com> writes:
hi friends , how can I declare / create function during runtime similiar to
lambda in lisp.


Probably by implementing a Lisp interpreter in C.

or a C interpreter in C:

http://www.softintegration.com/products/chstandard/

www.enlightenment.org has a nice implementation of Small, a C-like
language, called Embryo.
Apparently it's very fast, and contains a small subset of C.
Nov 14 '05 #10
"invincible" <mo**********@in.bosch.com> wrote:
# hi friends , how can I declare / create function during runtime similiar to
# lambda in lisp.

One way is to write the function to a file; call a compiler using system("cc ....")
to write a .so or .dll or .dylib or some other operating system specific format;
and then use operating system specific dynamic linking libraries to load it into
your address space.

Another way is to malloc a byte vector and then store the machine instruction code
in the vector. You then convert the data vector address to a function address and
call the function. However function addresses are not always the same as a data
address and you may need to combine a system specific linkage address to the code
address to get a function pointer. Also if you're using virtual memory, this would
often get a page protection fault unless you change the page characterisitics
in a system specific fashion.

Another possibility is to use an interpretter, perhaps a threaded interpretter.
Threaded interpretters are fast and once you have the internal interpretter written
(or simply find a Forth implementation written in C), easy to do. Threaded
interpretters don't run afoul of vm page protections.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!
Nov 14 '05 #11
On Thu, 19 May 2005 09:07:53 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Mark McIntyre <ma**********@spamcop.net> wrote:
On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>"invincible" <mo**********@in.bosch.com> wrote:
>
>> hi friends , how can I declare / create function during runtime similiar to
>> lambda in lisp.
>
>You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
>aware of without resorting to the most hairy of hacks.
You could write an interpreter, and your C programme could write the
fn out to file / into memory, invoke the interpreter on it, and
process the results.


Sure, but that's a whole different thing.


He said he wanted to create a function during runtime, the above meets
the requirement. YMMV.
Then you're creating _another_ programming environment within the larger program,
So what? He didn't say this was forbidden.
the language of which may be deceptively similar to the one your main program is written in.
Again, so what?
AFAIAA all lambda functions in all Lisp-alikes create new functions as a
part of the main program itself.


I've no clue about lisp, you may be right, but IMO its irrelevant.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #12
Mark McIntyre <ma**********@spamcop.net> wrote:
On Thu, 19 May 2005 09:07:53 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Mark McIntyre <ma**********@spamcop.net> wrote:
On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:

>"invincible" <mo**********@in.bosch.com> wrote:
>
>> hi friends , how can I declare / create function during runtime similiar to
>> lambda in lisp.
>
>You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
>aware of without resorting to the most hairy of hacks.

You could write an interpreter, and your C programme could write the
fn out to file / into memory, invoke the interpreter on it, and
process the results.


Sure, but that's a whole different thing.


He said he wanted to create a function during runtime, the above meets
the requirement. YMMV.


He said he wanted it to be similar to lambda-functions in Lisp. I'm not
a Lisp expert, but TTBOMK Lisp lambda functions are fully functional
functions, equivalent to compile-time functions in all regards except
their time of creation. In C terms this would imply, for example, being
able to pass its address to qsort() and getting the right results.

Richard
Nov 14 '05 #13
On Fri, 20 May 2005 08:13:40 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Mark McIntyre <ma**********@spamcop.net> wrote:

He said he wanted to create a function during runtime, the above meets
the requirement. YMMV.


He said he wanted it to be similar to lambda-functions in Lisp.


IME 'similar to' doesn't mean 'identical to' but YMMV. Anyhoo, we're
approximately in agreement I suspect.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so...
5
by: invincible | last post by:
hi friends , how can I declare / create function during runtime similiar to lambda in lisp. thanks Mohan
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
3
by: takarimasu | last post by:
How can i create an input object (text area,select) at runtime ? B.
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
3
by: leon | last post by:
hello friends, i am writing a page aspx and creating controls dinamicaly and then i must to create for each control the events as well. Anybody to know how????? happy day lion
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
5
by: Lance | last post by:
I need to create a Drawing.Bitmap from an array of integer values. My current technique creates a bitmap that eventually becomes corrupt (i.e., the bitmap's pixels change to a different color...
6
by: py_genetic | last post by:
Is this possible or is there a better way. I need to create a new class during runtime to be used inside a function. The class definition and body are dependant on unknows vars at time of exec,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.