0

Actually I am hooking memory management functions but one problem that confronts 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.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
user364625
  • 71
  • 1
  • 1
  • 4

2 Answers2

1

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)
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
1

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.

Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51