Archived

This topic is now archived and is closed to further replies.

Saving compiled code

This topic is 5378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! I want to save compiled code but don''t know how it works. For example: void test() { printf("Hello!"); } void* pvoid; void main() { pvoid = (void*)test; FILE* f; f = fopen("test.txt","w+b"); // saving my void here!! fclose(f); } Okay, hope you understand what I want to do. I can save the void, that''s not the problem but how do I know which size it has? The sizeof() operator doesn''t work so... Can anyone help me?? Greetings Corrail

Share this post


Link to post
Share on other sites
I''m sorry, I don''t think anyone here is going to be able to figure out specifically what you''re trying to do.

Are you trying to write the code for the test function to a file? If so, then I''d like to say that the only reason I can think of to do this would be for a virus. Otherwise, why?

Secondly, you''ve already got the compiled code in a file, the file that is generated from the compiler (most likely an EXE).

Share this post


Link to post
Share on other sites
No, I''m not going to write a virus.

I want to create a model file format with functions included. I want to try it because of dynamics and speed advantages! I want to save different functions for each model.

Corrail
corrail@gmx.at
ICQ#59184081

Share this post


Link to post
Share on other sites
I don''t think I can help much with saving compiled code like that into a model''s file. Seems like far more trouble than it''s worth. How many of these specialized functions do you really expect, and what do you want to compile them? Are your modellers really going to spend their time fighting with a tool to try to create these functions?

Share this post


Link to post
Share on other sites
The Model Editor is going to be another problem... :-)

I don''t think that there''re many functions I want to save. Per model 3 or 4.

Corrail
corrail@gmx.at
ICQ#59184081

Share this post


Link to post
Share on other sites
You won''t be able to do it like that with machine code compiled functions (at least not with a *lot* of headaches), memory protections, global memory layouts etc. all mean it is hard and very non-portable.

A better solution would be to use some sort of byte-code compiled language (e.g. LUA or a Lisp dialect, or Java, or any number of other languages), even then the functions will have restrictions on them.

Share this post


Link to post
Share on other sites
in your function:


void test() {
printf("Hello!");
};


void endOfTest() {
};


void (* funct)();

void main() {
funct = test;

int size = (int)(endOfTest - test);

FILE * f = fopen("test.txt","w+b");

fwrite(funct, 1, size, f);

fclose(f);
};


Basically, enfOfTest is a function directly after your test function. the compiler will align them right after each other in assembly codes. good luck

www.cppnow.com

Share this post


Link to post
Share on other sites