C++ and dynamic programming

T

The Directive

Does C++ support dynamic programming? I hope I'm using the correct
term. I want to write code that can dynamically rewrite itself! I want
to dynamically create functions and call them and etc. If not, are
there any plans to add support for it in the future? What other
popular programming languages support dynamic programming? [Email me
privately for the last question so that people will not complain that
it's out of context.]

--The Directive
 
J

John Carson

The Directive said:
Does C++ support dynamic programming? I hope I'm using the correct
term. I want to write code that can dynamically rewrite itself! I want
to dynamically create functions and call them and etc. If not, are
there any plans to add support for it in the future? What other
popular programming languages support dynamic programming? [Email me
privately for the last question so that people will not complain that
it's out of context.]

If you mean does C++ allow you to generate functions on the fly that are
identical to those produced at compile time, then the answer is no. C++ can,
however, be used to create an interpreter, i.e., a program that will allow
you to type in functions at runtime and will store and execute those
functions. Writing such a program involves some work, however (how much work
depends on the range of possible functions you wish to support).

Any function that you might write will be built up from basic operations
(built in operators like + as well as predefined functions like sin and
cos). Accordingly, an interpreter basically consists of a pre-written
all-purpose function that calls basic operations as needed by the function
that the user specifies at run-time (in the simplest case, it may involve a
switch statement that executes different operations depending on which
operation has been specified at run-time). For a simple illustration of how
this is done, look at the calculator program in Ch 6 (including exercise 20)
of Stroustrup's TC++PL.
 
S

Sharad Kala

John Carson said:
The Directive said:
Does C++ support dynamic programming? I hope I'm using the correct
term. I want to write code that can dynamically rewrite itself! I want
to dynamically create functions and call them and etc. If not, are
there any plans to add support for it in the future? What other
popular programming languages support dynamic programming? [Email me
privately for the last question so that people will not complain that
it's out of context.]

If you mean does C++ allow you to generate functions on the fly that are
identical to those produced at compile time, then the answer is no. C++ can,
however, be used to create an interpreter, i.e., a program that will allow
you to type in functions at runtime and will store and execute those
functions. Writing such a program involves some work, however (how much work
depends on the range of possible functions you wish to support).

Well , C++ can generate class hierarchies on the fly.
That means you can make the compiler generate it for you.
This involves using templates.
Take a look at Andrei Alexandrescu's "Modern C++ design" where he shows in
chapter 4 how to
generate linear and scattered class hierarchies at compile time.

Best wishes,
Sharad
 
J

Jacques Labuschagne

Sharad said:
Take a look at Andrei Alexandrescu's "Modern C++ design" where he shows in
chapter 4 how to
generate linear and scattered class hierarchies at compile time.
^^^^^^^^^^^^^^^
"Compile time" may or may not meet your definition of dynamic
programming, because it involves a compile/execute/modify-source loop.
It doesn't happen on what most people consider to be "the fly".

Jacques.
 
G

Gianni Mariani

The said:
Does C++ support dynamic programming? I hope I'm using the correct
term. I want to write code that can dynamically rewrite itself! I want
to dynamically create functions and call them and etc. If not, are
there any plans to add support for it in the future? What other
popular programming languages support dynamic programming? [Email me
privately for the last question so that people will not complain that
it's out of context.]

--The Directive

C++ has no -built-in- support for dynamic programming however, it's very
easy to do if you have an installed compiler. See below:


#include <iostream>

struct Foo;
Foo * holder;

struct Foo
{
virtual void Set( const char * x ) = 0;
virtual void Echo() = 0;
};

const char program[] =
"\n"
"struct Foo\n"
"{\n"
" virtual void Set( const char * x ) = 0; \n"
" virtual void Echo() = 0; \n"
"};\n"
"\n"
"#include <iostream>\n"
"\n"
"extern Foo * holder;\n"
"\n"
"struct Zoo : Foo\n"
"{\n"
" Zoo()\n"
" {\n"
" std::cout << \"I'm a foo\\n\";\n"
" holder = this;\n"
" }\n"
"\n"
" const char * str;\n"
"\n"
" virtual void Set( const char * x )\n"
" {\n"
" str = x;\n"
" }\n"
"\n"
" virtual void Echo()\n"
" {\n"
" std::cout << \"Zoo echos \" << str << \"\\n\";\n"
" }\n"
" \n"
"};\n"
"\n"
"Zoo myzoo;\n"
"\n";


#include <fstream>
#include <dlfcn.h>

int main()
{
const char progfilename[] = "dynprog-adder.cpp";
const char dsofilename[] = "./dynprog-dso.so";

std::eek:fstream progfile( progfilename );

progfile.write( program, sizeof( program ) -1 );

progfile.close();

std::string command = std::string( "g++ -shared -o " ) +
dsofilename + " " + progfilename;


system( command.c_str() );

void * dl_hanle = dlopen( dsofilename, RTLD_LAZY );

holder->Set( "Hello world" );

holder->Echo();
}
 
T

The Directive

John Carson said:
The Directive said:
Does C++ support dynamic programming? I hope I'm using the correct
term. I want to write code that can dynamically rewrite itself! I want
to dynamically create functions and call them and etc. If not, are
there any plans to add support for it in the future? What other
popular programming languages support dynamic programming? [Email me
privately for the last question so that people will not complain that
it's out of context.]

If you mean does C++ allow you to generate functions on the fly that are
identical to those produced at compile time, then the answer is no. C++ can,

So is C++ dynamic programming the correct term? I want my program to
be able to modified (rewrite) itself depending on input values.

--The Directive
 
R

red floyd

The said:
John Carson said:
Does C++ support dynamic programming? I hope I'm using the correct
term. I want to write code that can dynamically rewrite itself! I want
to dynamically create functions and call them and etc. If not, are
there any plans to add support for it in the future? What other
popular programming languages support dynamic programming? [Email me
privately for the last question so that people will not complain that
it's out of context.]

If you mean does C++ allow you to generate functions on the fly that are
identical to those produced at compile time, then the answer is no. C++ can,


So is C++ dynamic programming the correct term? I want my program to
be able to modified (rewrite) itself depending on input values.

The term is "self-modifying code", and it's generally a bad idea.

1. Most modern platforms disallow this.
2. It's not robust, and very difficult to maintain.
3. It's difficult to debug.
 
S

Sharad Kala

Jacques Labuschagne said:
^^^^^^^^^^^^^^^
"Compile time" may or may not meet your definition of dynamic
programming, because it involves a compile/execute/modify-source loop.
It doesn't happen on what most people consider to be "the fly".

Yes..actually I do agree with you that it isn't exactly what people would say
dynamic programming.
In fact it uses static polymorphism.
My point was that you can make C++ to write code for you. This is unlike virtual
functions where the branch of execution to be selected
is dynamic, but the code for execution has already been hand-written by someone.

Best wishes,
Sharad
 
P

peter

The said:
Does C++ support dynamic programming? I hope I'm using the correct
term. I want to write code that can dynamically rewrite itself! I want
to dynamically create functions and call them and etc. If not, are
there any plans to add support for it in the future? What other
popular programming languages support dynamic programming? [Email me

your problem might be resolved using embeddable C/C++ interpreter Ch. It
allows you to generate C/C++ code source code/functions on the fly and
be called by your C/C++ binary application, or call back your binary
C/C++ functions.

you can take a look at
http://www.softintegration.com/solution/embedded/
http://www.softintegration.com/products/sdk/embedded_ch/

privately for the last question so that people will not complain that
it's out of context.]

--The Directive
 

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,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top