Possible to allocate a small function at run-time?
Posted: Thu Mar 21, 2013 4:56 am
Is it possible to allocate a small function at run-time?
I don't have much of a reason to want to do this that couldn't be done by a macro, but I was thinking of this:
entity->Think();
Where the allocated function goes like this:
I could just as easily write a macro:
#define THINK_ENT(_ent) _Think(_ent) // Yes this is a stupid macro, but I'm thinking of more complex applications of this ultimately with more arguments...
Or call the function directly. I just enjoy exploiting C capabilities to the max. Sadly, during the course of writing this, I've already thought of a way to maybe explore this that goes like this ....
Then I get the memory address of Think1 and Think2 and Think3 and copy the piece of memory that Think1 uses (&Think1 to &Think2) using memcpy to locate the pointer address that needs changed (and it seems to me that would be platform independent).
Thoughts? Would I accidentally piss off anti-virus or something by doing this or make an operating system security model mad at me that maybe uses certain addresses for execution and certain addresses for storage?
I don't have much of a reason to want to do this that couldn't be done by a macro, but I was thinking of this:
entity->Think();
Where the allocated function goes like this:
Code: Select all
void Think(void)
{
entity_t* me = &entity[52];
_Think(me)
}#define THINK_ENT(_ent) _Think(_ent) // Yes this is a stupid macro, but I'm thinking of more complex applications of this ultimately with more arguments...
Or call the function directly. I just enjoy exploiting C capabilities to the max. Sadly, during the course of writing this, I've already thought of a way to maybe explore this that goes like this ....
Code: Select all
void Think1(void)
{
entity_t* me = &entity[0];
_Think(me)
}
void Think2(void)
{
entity_t* me = &entity[1];
_Think(me)
}
void Think3(void)
{
entity_t* me = &entity[2];
_Think(me)
}Thoughts? Would I accidentally piss off anti-virus or something by doing this or make an operating system security model mad at me that maybe uses certain addresses for execution and certain addresses for storage?