2

I would like to do the following . I have declared a structure in my program and in run time when the program is being executed, if there is a user input, I should be able to create another new structure/edit that structure in my code. How can we do that? Is this what "A self modifying code"? Please clarify .Please give some examples .Thanks

Let me give an idea of what I wish to do , I have a "Structure/Class " called "student", which contains variables like "int roll_no" and "int reg_no". If the user wishes to add a new variable like "char name" in run time how can it be done?

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
Niranjan Sonachalam
  • 1,545
  • 1
  • 20
  • 29
  • This is possible - but depends on the language you are using. Are you using C, PHP, etc? Also, are you intending to create new code from user input? If so, you need to be very careful about what you accept - especially over the web - for security reasons. Perhaps if you can show your use case, you can get some more accurate advice. – halfer Oct 16 '11 at 08:02
  • I am using C++, Is this possible in C? And what language will be easy for a self modifying code?What about Python? This will be a offline system – Niranjan Sonachalam Oct 16 '11 at 08:10
  • as far as I know, easiest way for C/C++ is to generate code, compile it to libraries and then load in runtime. However python or any other dynamic language is much better choice for this. – Kamil Tomšík Oct 16 '11 at 08:14
  • @Niranjan, with C you can either use `tcc` or, better, `LLVM` for runtime code generation. Both are very easy to use. – SK-logic Oct 16 '11 at 15:08

2 Answers2

3

Have a look on this:

http://bracha.org/Site/Talks.html

There is talk about reflection, which is probably what you want - reflection is not only about introspection (which most of developers already knows) but also about changing meaning of language constructs and runtime code manipulation.

Best languages for this kind of stuff are probably ruby and smalltalk.

If your language does not support these capabilities, you have still option to leverage code-generation - which is possible in almost every programming language but it is much easier in dynamic ones with "eval" support. For example this kind of stuff is possible even in C/C++ but your app has to embed compiler.

Java is good choice too (thx to classloaders and a lot of libraries for bytecode manipulation)

Oh, I've almost forgot, have a look on lisp and metacircular evaluation.

Kamil Tomšík
  • 2,415
  • 2
  • 28
  • 32
0

Sounds like you don't need to modify the existing code, but rather emit some new code in runtime. It is easy to do with any environment where your compiler is present in runtime. It is true for .NET, for JVM-based environments, various Lisps, etc.

SK-logic
  • 9,605
  • 1
  • 23
  • 35