create function at runtime

I

invincible

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

thanks

Mohan
 
R

Richard Bos

invincible said:
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
 
D

DeltaOne

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);
 
C

Chris McDonald

DeltaOne said:
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>
 
K

Keith Thompson

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

Probably by implementing a Lisp interpreter in C.
 
M

Mark McIntyre

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.[/QUOTE]

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.
 
F

Flash Gordon

Chris said:
<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.
 
R

Richard Bos

Mark McIntyre said:
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. [/QUOTE]

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
 
S

SM Ryan

# 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.
 
M

Mark McIntyre

On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,


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.[/QUOTE]

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.
 
R

Richard Bos

Mark McIntyre said:
Sure, but that's a whole different thing.

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

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
 
M

Mark McIntyre

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. [/QUOTE]

IME 'similar to' doesn't mean 'identical to' but YMMV. Anyhoo, we're
approximately in agreement I suspect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top