views:

155

answers:

7

Hello, in C++, I can easily create a function pointer by taking the address of a member function. However, is it possible to change the address of that local function?

I.e. say I have funcA() and funcB() in the same class, defined differently. I'm looking to change the address of funcA() to that of funcB(), such that at run time calling funcA() actually results in a call to funcB(). I know this is ugly, but I need to do this, thanks!

EDIT----------

Background on what I'm trying to do:

I'm hoping to implement unit tests for an existing code base, some of the methods in the base class which all of my modules are inheriting from are non-virtual. I'm not allowed to edit any production code. I can fiddle with the build process and substitute in a base class with the relevant methods set to virtual but I thought I'd rather use a hack like this (which I thought was possible).

Also, I'm interested in the topic out of technical curiosity, as through the process of trying to hack around this problem I'm learning quite a bit about how things such as code generation & function look-up work under the hood, which I haven't had a chance to learn in school having just finished 2nd year of university. I'm not sure as to I'll ever be taught such things in school as I'm in a computer engineering program rather than CS.

Back on topic The the method funcA() and funcB() do indeed have the same signature, so the problem is that I can only get the address of a function using the & operator? Would I be correct in saying that I can't change the address of the function, or swap out the contents at that address without corrupting portions of memory? Would DLL injection be a good approach for a situation like this if the functions are exported to a dll?

A: 

I am fairly sure this is impossible in pure C++. C++ is not a dynamic language.

Matthew Flaschen
+1  A: 

It cannot be done the way you describe it. The only way to change the target for a statically bound call is by modifying the actual executable code of your program. C++ language has no features that could accomplish that.

If you want function calls to be resolved at run-time you have to either use explicitly indirect calls (call through function pointers), or use language features that are based on run-time call resolution (like virtual functions), or you can use plain branching with good-old if or switch. Which is more appropriate in your case depends on your specific problem.

AndreyT
+4  A: 

No. Functions are compiled into the executable, and their address is fixed throughout the life-time of the program.

The closest thing is virtual functions. Give us an example of what you're trying to accomplish, I promise there's a better way.

GMan
it is possible using codecave on the "code segment" but it will be different field than "formal" programming.also, it will require both function to have similar prototype.
YeenFei
Thanks, that was an interesting read!
wk1989
+1  A: 

Technically it might be possible for virtual functions by modifying the vtable of the type, but you most certainly cannot do it without violating the standard (causing Undefined Behavior) and it would require knowledge of how your specific compiler handles vtables.

For other functions it is not possible because the addresses of the functions are directly written to program code, which is generally on a read-only memory area.

Tronic
+1  A: 

What you want is a pointer to a function, you can point it to FuncA or FuncB assuming that they have the same signature.

KPexEA
I do have the pointers, but the actual code uses function calls and I can't touch that.
wk1989
wk1989, the fact that you cannot change some of the code is a very important point that should have been in the original question. Live and learn.
KPexEA
A: 

You cannot do what you want to do directly. However, you can achieve a similar result with some slightly different criteria, using something you are already familiar with -- function pointers. Consider:

// This type could be whatever you need, including a member function pointer type.
typedef void (*FunctionPointer)();

struct T {
   FunctionPointer Function;
};

Now you can set the Function member on any given T instance, and call it. This is about as close as you can reasonably get, and I presume that since you are already aware of function pointers you're already aware of this solution.

Why don't you edit your question with a more complete description of the problem you're trying to solve? As it stands it really sounds like you're trying to do something horrible.

Josh Petrie
A: 

Its simple!

For

at run time calling funcA() actually results in a call to funcB().

write funcA() similar to following:

int funcA( int a, int b) {
  return funcB( a, b );
}

:-)

ArunSaha