How to get pointer name
Actually I am hooking memory management functions but one problem that con开发者_运维百科fronts me is that I am unable to get the pointer name for which memory is being allocated in my hooked function. I mean... suppose Myfunction() allocates memory to a pointer Myptr through new operator as geven below.
Myfunction()
{
int * Myptr = new int;
}
Now here I want that when My hooked function "Mynew" is called for memory allocation, it should know the name of the pointer Myptr for whom memory is being allocated.
This is not possible.
You could do something similar with a macro, so that it passes the variable name to a function:
#define NEWPTR(var, type) type * var = allocate_memory(type, #var)
The pointer name is only known at compile time, not at runtime. Also, the allocation new int
is an expression separate from its assignment to the Myptr
variable. You're redefining allocators, not the assignment operator for pointers (I hope!)...
As @Sjoerd suggests, you could get something like this for your own code by introducing a macro calling your own allocation function. I'm not sure if this is what you really need.
精彩评论