Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose we have a struct:

C++
struct Foo
{
    void(*Print)(); //could be any method pointer
    int MyField;
}


We can set Foo->Print to any function eg:
Foo->Print = Test;

C++
void Test()
{
   printf("print called");
}

void Callback()
{
   //this should be called for every memory access at Foo->Print
}


How do I implement a memory access callback so that every time Foo->Print is called, Callback also is invoked?
Memory profilers and debuggers can do exactly this so it should be possible.
It would be nice if this also works for simple fields like MyField for read and write.

What I have tried:

I dont know where to start -.-
Posted
Updated 7-Oct-16 22:45pm

1 solution

Debuggers use special instructions and operating system services to accomplish this. If you want to remain independent of the processor and operating system your choices are much more limited.

One way of implementing this is to introduce a second function pointer; let's call it PrintWrapper. The outside world should only call PrintWrapper. And in this function you call Print and call Callback (or do all those task of Callback directly in PrintWrapper). That is a platform independent way of doing it and really simple.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900