How to convert string (pointer to char) to pointer to function

Category: visual studio vc

Question

forever a loser on Tue, 24 May 2016 15:13:39


For example:

#include <stdio.h>
#include <memory.h>
typedef int(*two_ints_to_int)(int,int);
void* compile(char* s);
void main()
{
    two_ints_to_int sum = (two_ints_to_int)compile("int sum(int a, int b) { return a + b; }");
    printf("5 + 3 = %d", sum(5,3));
    free(sum);
}

The output of this program should be: 

5 + 3 = 8

Explanation:

compile is a function that receives C code of a function as string (pointer to char), compiles it, allocates virtually and dynamically during run time the appropirate executable memory on current process memory, and then returns it's address as a void pointer, like what malloc and calloc do, but only read and write memory. Then I should interpret this void pointer as a pointer to the appropirate function, exactly what C programmer must do when using malloc or calloc. Finally I must free all what compile allocated, i.e. all bytes that it allocated, when the new function is no longer needed. Exactly the same after using malloc or calloc, you must free them when the memory they allocated is not used anymore. If the function, described in the string, uses outer function, like printf, and compile doesn't know what is the address of the used outer function, then I can variadically give the address of the used outer function as another variadic parameters.

For instance:

#include <stdio.h>
#include <memory.h>
typedef void(*action)(void);
void* compile(char* s, ...);
void main()
{
    action print = (action)compile("void print() { printf(\"I like compile function.\"); }", "printf", &printf);
    print();
    free(print);
}

The output of this program should be:

I like compile function.

When compile finds the string "printf(", then it realizes that printf is a function that the allocated print should call, but it needs it's address. The address is given in the third parameter &printf. I also gave it the string "printf", to tell compile that "printf" is a function and &printf (the parameter after) is the address of this function, so when it encounters "printf" in the code (given in the first parameter), it knows that the allocated function print must calls function at the address &printf (given in the third parameter). It is bit similar to sprintf and sprintf_s, that they are also variadic functions, like compile.

Exactly the same thing when I want to use outer variables, like global variables but also local variables, in the allocated function.

For example:

#include <stdio.h>
#include <memory.h>
typedef void(*action)(void);
void* compile(char* s, ...);
void main()
{
   int integer = 123;
   action A = (action)compile("void A() { printf(\"integer = %d\n\", integer); integer = 321; }", "printf", &printf, "integer", &integer);
   printf("but function \"A\" modified it and now it is equal to %d", integer);
   free(A);
}
The output of this program should be: 

integer = 123
but function "A" modified it and now it is equal to 321

The problem is that exist compilers can open C file and create appropirate exe file, but I didn't find an equivalent function, like compile, that gets string (pointer to char) and returns pointer to function (executable memory).

Replies

Baron Bi on Wed, 25 May 2016 09:22:19


Hi

Thanks for posting here.

 

There seems has no such a function in native c++ can do this directly. However, you can make an associative container (like a std::map) associating strings to member function pointers, and use it for wrapping and unwrapping. You can also try CodeDomProvider Class, that may be helpful.

 

Best Regards,

Sera Yu

forever a loser on Fri, 27 May 2016 21:54:53


Thanks for your reply

Baron Bi on Mon, 30 May 2016 06:55:25


You're welcome.

If my reply can help you solve your case, please mark and finish this case. If not, just let us know and we will find better solutions to help you. Thanks.

forever a loser on Tue, 31 May 2016 18:52:23


I know already about the method to store functions and codes in associative container (like a std::map), but I don't want to use any data structure and I don't want to use NET assemblies. Your suggestions are good, but I am programming in C language, and in C I can't use neither std::map nor NET assemblies. As I understood you, there is no function in the standard library of C and C++ that can compile string code to pointer to function, but C and C++ programs don't have to use only C and C++ standard libraries. There are much another libraries that people have done that you can download from the internet and use, like SDL, libpng, lipjpeg, etc. I am pretty sure that I can code by myself the compile function in assembly, as how people coded the C compiler, but to convert string to pointer to function (executable memory), instead of converting C files to exe files. If I want to do this natively, I must code myself the compile function. Am I right?

ranta on Wed, 01 Jun 2016 15:38:33


Tiny C Compiler (TinyCC or TCC) provides the libtcc library for compiling C code at run time and optionally executing the results. There apparently is a Win32 port. Check whether the license fits your business requirements.

The Clang subproject of LLVM may also have a similar feature, but I guess its API is more complex.