This is using the C langauge with VC++ 2010 on windows 7 64 bit.
Is there a way to reliably and consistently access the address of a function (c linkage) so that it's the same address on every run of your program?
This is using the C langauge with VC++ 2010 on windows 7 64 bit.
Is there a way to reliably and consistently access the address of a function (c linkage) so that it's the same address on every run of your program?
If you got the address at runtime, it should remain stable for that run. It may be different next time.
There are features in the OS that may cause your executable code to be loaded at unpredictable addresses (partly for security, partly because two DLLs may want the same base address).
No, because there is no guarantee the function would be loaded at the same address each time for most modern OSes. On some embedded devices you might be able to get away with this, provided your linker file defines a fixed address for the function.
But why bother? You can just take the address of the function like you would a variable's:
void func(int a)
{
a=5;
}
void main()
{
// In case you don't know or don't care about the signature of the function.
void* p = func;
// In case you do care and might want to call the function
// at some point through the pointer.
void (*f)(int) = func;
printf("%p %p",p, f);
}