View previous topic - View next topic |
Author |
Message |
tcaudilllg Dragonmaster
Joined: 20 Jun 2002 Posts: 1731 Location: Cedar Bluff, VA
|
Posted: Wed Nov 23, 2005 12:06 am Post subject: |
[quote] |
|
So what you are saying is, I can do this:
1) define a character array/string in C. (not constant)
2) define a C implementation as a part of a program
3) compile C code strings the program is designed to create in response to user events into ASM binary with an implemented C compiler (as part of the program)
4) store the binary into the string created at 1)
5) assign a function pointer the address of the binary and execute it, meaning C can act as a self-modifying, wholly dynamic programming language a la Javascript.
Yes?
|
|
Back to top |
|
|
RuneLancer Mage
Joined: 17 Jun 2005 Posts: 441
|
Posted: Wed Nov 23, 2005 1:52 am Post subject: |
[quote] |
|
The question is, why would you want to do this (if that was even close to what a function pointer does to begin with)? Your users are not going to write C code in your program and there's hardly a point to putting your code in a string other than self-modifying code. Which would not be possible as short of building an interpretor that would attach to your program, code does not magically compile itself on its own from a string to working code. Otherwise, why the hell would we be wasting time with these ridiculous "compiler" things? :P It just doesn't work that way.
Function pointers allow you to specify a number of functions which have the same prototype and then store the address of one of these functions in a variable. The function can then be called without knowing which one you're actually dealing with, and allowing you to switch between functions on the fly by reassigning a new function to this pointer when the need arises.
But then again, you might as well just learn what "inheritence" means and go the OOP way with C++...
Edit: ...Javascript is interpreted. C is not. There's a HUGE difference right there. Although I'd love to know why you'd want to write self-modifying code. Viruses are pretty much the only application that comes to mind that can't be replaced by a safer, more standard method of doing things. _________________ Endless Saga
An OpenGL RPG in the making. Now with new hosting!
|
|
Back to top |
|
|
LeoDraco Demon Hunter
Joined: 24 Jun 2003 Posts: 584 Location: Riverside, South Cali
|
Posted: Wed Nov 23, 2005 7:37 am Post subject: |
[quote] |
|
LordGalbalan wrote: | So what you are saying is, I can do this:
1) define a character array/string in C. (not constant)
2) define a C implementation as a part of a program
3) compile C code strings the program is designed to create in response to user events into ASM binary with an implemented C compiler (as part of the program)
4) store the binary into the string created at 1)
5) assign a function pointer the address of the binary and execute it, meaning C can act as a self-modifying, wholly dynamic programming language a la Javascript.
Yes? |
No. Although, you can do something slightly like that with a Just-in-time compiler.
Example of function pointers:
Code: | int foo( int qux )
{
return qux + qux;
}
int bar ( int qux )
{
return qux * qux;
}
void computeAndPrint( int param, int (*func)( int ) )
{
printf( "value of func with parameter %d = %d\n", param, func( param ) );
}
int main( int argc, char **argv )
{
computeAndPrint( 5, foo ); /* prints 10 */
computeAndPrint( 5, bar ); /* prints 25 */
return EXIT_SUCCESS;
} |
Er, that should hopefully work; I haven't checked the code. There might be a slight issue with the syntax of the second parameter of "computeAndPrint". But that's more or less the idea: provide a mechanism for allowing functions to be objects.
(Perhaps not the best example possible, but it should showcase what is going on.)
All of which is discussed in the third link I provided. (You did look at that, right? Although, had you done so, you would not have come up with that silly question...) _________________ "...LeoDraco is a pompus git..." -- Mandrake
|
|
Back to top |
|
|
tcaudilllg Dragonmaster
Joined: 20 Jun 2002 Posts: 1731 Location: Cedar Bluff, VA
|
Posted: Fri Nov 25, 2005 10:23 am Post subject: |
[quote] |
|
Really, I don't plan on trying the idea I presented. I just wanted to make sure that we were all really understanding all that function pointers could do, because what Janus said (and you backed this up, LeoDraco) was pretty much the following: "function pointers point to -any- binary data as executable code". And yes, RL, I did remember to include an internal compiler as native to the application for the purpose of running the code.
Why would I even think it? Well... to try implementing that "pick-up-and-set-down-blocks-for-the-purpose-of building-algorithms-" idea, of course....
|
|
Back to top |
|
|
RuneLancer Mage
Joined: 17 Jun 2005 Posts: 441
|
Posted: Fri Nov 25, 2005 8:12 pm Post subject: |
[quote] |
|
For Dyne, I'm guessing? I can see where you're going with this, though at this point you'd probably be safer making a compiler (or at least interpretor) out of it and letting the user link to plugins that support different languages. Your users could write code into "blocks" and not have to worry about conforming to a syntax that doesn't suit their environment...
Granted, that's easier said than done. :P But it probably wouldn't take too much work linking to a C/C++ compiler and having that worry about the actual naughty bits, whereas you'd just have to worry about getting your blocks to do what they have to do. _________________ Endless Saga
An OpenGL RPG in the making. Now with new hosting!
|
|
Back to top |
|
|
|
Page 2 of 2 |
All times are GMT Goto page Previous 1, 2
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|