auto detect variable value change - is it possible?

This is a discussion on auto detect variable value change - is it possible? within the C++ Programming forums, part of the General Programming Boards category; Is there some way that I can "hook" a variable so that when it's value changes, some sort of callback ...

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,608

    auto detect variable value change - is it possible?

    Is there some way that I can "hook" a variable so that when it's value changes, some sort of callback is called that lets me know? I thought of making a loop that constantly checks the variable in a different thread, but this would hurt performance pretty bad. Is there a way I can accomplish this without sacrificing much performance?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Implement the Observer design pattern.

    Keep the the value in an observable class and have the observers register themselves with the observable. When it changes, call observer->notify(), or whatever you name it, for all registered observers.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    No portable way to even go about that that I know of.

    Your best bet is to monitor the variable by way of a class or a function, but I suspect you're trying to monitor a variable in another application.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,608
    The thing is I want a DLL to automatically free resources that it's user-app created. It would do this when the specified pointer changed to something else. So I guess it's actually a local variable. Do you know of a better way of doing this? (The "Observer design pattern" appears to be out of my league!)
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can't be done. On a limited number of variables within one process, you could, at least in theory (and limiited to certain processor architectures) set up a memory write breakpoint, but that's only useful for up to 4 memory addresses in x86 - other processors may have a different number, but it's not going to work for 100s or 1000s of pointers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  2. I thought I understood but....(pointer question)
    By caroundw5h in forum C Programming
    Replies: 1
    Last Post: 02-22-2005, 02:05 AM
  3. How to detect change in sign?
    By bugsmashers in forum C++ Programming
    Replies: 16
    Last Post: 02-20-2005, 06:27 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21