Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Address of function is not actual code address

Tags:

Debugging some code in Visual Studio 2008 (C++), I noticed that the address in my function pointer variable is not the actual address of the function itself. This is an extern "C" function.

int main() {    void (*printaddr)(const char *) = &print; // debug shows printaddr == 0x013C1429  }  Address: 0x013C4F10 void print() {   ... } 

The disassembly of taking the function address is:

   void (*printaddr)(const char *) = &print; 013C7465 C7 45 BC 29 14 3C 01 mov         dword ptr [printaddr],offset print (13C1429h)  

EDIT: I viewed the code at address 013C4F10 and the compiler is apparently inserting a "jmp" instruction at that address.

013C4F10 E9 C7 3F 00 00   jmp         print (013C1429h)  

There is actually a whole jump table of every method in the .exe.

Can someone expound on why it does this? Is it a debugging "feature" ?

like image 590
codenheim Avatar asked Mar 21 '10 00:03

codenheim


People also ask

What is the address of a function?

A function is a block of code that is defined to perform a specific piece of work in a program. It is used to ease the work of the programmer by defining a commonly occurring piece of code so that it can be reused when required. The address is the memory location where the entity is stored.

Do functions have address C?

Address of a function in C or C++ In C or C++, the variables are stored into memory, so we can get their memory addresses. Similarly, the functions also are stored into the memory, so they also have some addresses. To get the address we can use the function name only without using the parenthesis.

How do you print the address of a function in C++?

how to print the address of a member function using pointer to member function through a object of that class. class MyClass { public: void fun(void){}; }; void (MyClass::*ptr)(void); void main(void) { MyClass myObject; ptr = MyClass::fun; cout << myObject. *ptr << endl; // ... }

Where is the address of a function stored?

Like normal data pointers, we have function pointers that point to functions . The address of a function is stored in a function pointer.


1 Answers

That is caused by 'Incremental Linking'. If you disable that in your compiler/linker settings the jumps will go away.

http://msdn.microsoft.com/en-us/library/4khtbfyf(VS.80).aspx

like image 74
RaptorFactor Avatar answered Sep 23 '22 01:09

RaptorFactor