How to know if a variable has been accessed

I wondered if anyone knew how to tell whether a variable has been accessed in memory, no matter what it's been used for... Whether it's actually been set to something else, or whether it's been get for an operation or function call that doesn't actually do anything to affect the variable itself. Either way the variable has been "accessed" for something.

I'm not entirely certain that it's possible to detect a program getting the variable but I know that programs exist where they can trace what has accessed a certain part of memory... CheatEngine is one example, although I'm not entirely sure whether this can only detect changes in the variable and then trace what did it.

It is possible to track if the value has changed but I don't know how possible it is to determine when the value is just read (without modifying it), plus it seems like unnecessary work.

You can achieve the tracking by retaining a shared_pointer object to the value
Sounds like what you need is a debugger.

(dbx) stop access r &x,sizeof(x)                                             
(2) stop access ra &x, sizeof(x)
(dbx) run                       
Running: test 
(process id 3760)
watchpoint ra &x, sizeof(x) (0x20e20[4]) at line 8 in file ""
    8       printf("%d", x);
(dbx) where
=>[1] g(), line 8 in ""
  [2] f(), line 13 in ""
  [3] main(), line 18 in ""
If the variable is encapsulated within an object you can have a means of logging the access to a property by having a function within your get { } accessor.
Last edited on
The variable isn't encapsulated, it's just a standard C++ primitive variable.
Also I thought using the get property was only a Java/C# thing... I've not really looked into it that much so I don't really know.

Smac89, cheers, not entirely sure if it's answer to the question I was asked but that's exactly what I need for my code (again I've not really looked into these either)... Suppose I was going about it in an awkward way.
Topic archived. No new replies allowed.