0

I have a use-case in which the function contents are to be selected conditionally. And that function pointer is passed to other module for later invocation.

void encode_function(int flag)
{
 if (flag == 1)
  encode1();
 if (flag == 2)
  encode2();

  encode_defaults();
}

Once the encode_function() is populated, I will pass it to other module, where it will be invoked. I am trying to achieve it in C language, but no success so far. I tried to look at dyncall library but it supports only dynamic parameter changes.

I am looking for something which allows me to change the function contents at run-time.

Some existing question Is there a way to modify the code of a function in a Linux C program at runtime?

Community
  • 1
  • 1
bharat
  • 29
  • 4
  • 9
    `change the function contents at run-time` why don't have separate functions and use function pointer array to select the function to be called at runtime? – Sourav Ghosh Feb 23 '15 at 16:35
  • 1
    Generate code and compile it as a DLL/Shared library and load the dll at runtime? – BitTickler Feb 23 '15 at 16:36
  • 2
    Do you really need to change the function contents at run time? Or, will selecting from a few known functions suffice? – Degustaf Feb 23 '15 at 16:38
  • If speed is not that much of a concern, you can also integrate V8 or LUA etc. into your application and simply execute new versions of the scripts. – BitTickler Feb 23 '15 at 16:38
  • There are ways to do it in other languages that has functional programming support. The best way I can think of to do it in C is to wrap the parameter and function pointer together in a struct and send them together. – user3528438 Feb 23 '15 at 16:41
  • You could invent your own Domain specifiic mini language and write a small interpreter for it. Then you could simply give the function a new "dsl program" to interpret. This is not necessarily slow or much work. With like 500 lines you already are in the game. – BitTickler Feb 23 '15 at 16:43
  • how do you know which content you are changing the function to? if everything can be determined in compile time, why not a switch case? – Jason Hu Feb 23 '15 at 16:46
  • @Sourav Ghosh Due to large no of conditions, using a function pointer array is not feasible. – bharat Feb 23 '15 at 16:51
  • @user2225104 I am trying to change unix kernel code for performance gains (should have mentioned earlier). – bharat Feb 23 '15 at 16:51
  • Then write and load new kernel modules. – BitTickler Feb 23 '15 at 16:52
  • @user3528438 hmm let me try if this is feasible, can you point me to some examples. – bharat Feb 23 '15 at 16:52
  • http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf <<-- Kernel module programming guide (PDF) – BitTickler Feb 23 '15 at 16:54
  • I am trying to club multiple file system operation and send it over to network server. For every set of operations if I try to load/unload a module, it is a bad design. – bharat Feb 23 '15 at 16:55
  • So, you want to partially apply a function, optimize that, and then pass a pointer to it? – Samuel Edwin Ward Feb 24 '15 at 16:46
  • I did this by adding all the function pointers to a struct list, pass the list to a a generic function which can be invoked later. It served my purpose. Thanks @user3528438 for the solution. – bharat Feb 26 '15 at 03:11

1 Answers1

3

You are basically looking for the ability to treat code as data, which is a feature rarely available in compiled imperative static languages like c. The capability of modifying functions at run time is generally categorized as high order functions (you're basically trying to return a function as data somewhere in the code).

If the problem can be solved by several static functions, you can pass in function pointers to different implementations, otherwise I think c does not have the ability to really treat code as data and modify them on the fly.

This question makes attempt to implement high order function in c, but going this far might not be what you want.

Community
  • 1
  • 1
miushock
  • 1,087
  • 7
  • 19
  • Hmm.. looks like there is no development being done to C language. – bharat Feb 23 '15 at 17:04
  • 1
    @BharatSingh languages may look like car series to you in the manner of constant remodeling and adding new features every a couple of years. but that really is not the case. The purpose of c is to carry out effective manipulation with in a chunk of memory, while other languages has other purpose, such as fast oo modeling on business logic. You might need a course on compiling theory to get a solid grasp on this topic. – miushock Feb 23 '15 at 17:27