Thread: How to know when a value of a variable change?

  1. #1
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46

    How to know when a value of a variable change?

    Dear all,
    I have a big program. A variable change unexpectedly, so I want to find what function change this variable.
    How can I track a variable in Linux environment?
    Is there any method?
    Any help are welcome.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can watch it in a debugger, but the bigger problem is that access to the variable isn't strictly controlled (ie. it's probably global). This generally makes tracking changes more difficult, as you've independently discovered.
    My best code is written with the delete key.

  3. #3
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    I mean, I want to set a trigger, a function will run when a variable changes.
    Can I do it?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your best bet is to simply call a function with the new value...

  5. #5
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    But, when do I should call this function?
    I want this function runs when the variable changes.
    Can I do it?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hannibal2010 View Post
    I mean, I want to set a trigger, a function will run when a variable changes.
    Can I do it?
    In general, no. In a debugger, you can set something like a "watchpoint" (the name and method to set one depend on the debugger) so, when the variable changes, execution is suspended at the instruction that caused the change.

    If you want to somehow modify your code so a function will be called if a variable changes then the possibilities are remote (for example, is creating a thread which monitors the value that you know is being changed unexpectedly). The problem is that changes the layout of your program itself in memory (as both the thread function and the method of creating a thread consume some resources). A side effect can therefore be that a different variable starts being changed.

    There is a very good reason that people actively discourage hacking of code, and debugging. Yes, it is difficult to design code does not clobber variables it shouldn't. But it is actually harder, particularly in large programs, to find the culprit when some variable is unexpectedly being changed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Hannibal2010 View Post
    But, when do I should call this function?
    I want this function runs when the variable changes.
    Can I do it?
    You would call the function wherever the variable is being changed... that is, instead of a = 10 ... call a function that changes a and does the other stuff you need done because it was changed.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Have a look at the original post, Tater. Existing program with a variable being unexpectedly changed. Wanting to somehow call a function when that unexpected change occurs.

    Hence Prelude's recommendation (and mine) to use a debugger.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    Have a look at the original post, Tater. Existing program with a variable being unexpectedly changed. Wanting to somehow call a function when that unexpected change occurs.

    Hence Prelude's recommendation (and mine) to use a debugger.
    Well, yes... I take your point... I had something else in mind; thinking of a variable being changed by a second thread and trying to catch it somehow...

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Why not do it in the way the debugger itself does it?
    ...(AFAIK) ..by running the monitored code as a inferior( child?) process to the monitoring code.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why not do it in the way the debugger itself does it?
    Why not just use a debugger instead of reinventing the wheel with no good reason?
    My best code is written with the delete key.

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Prelude View Post
    Why not just use a debugger instead of reinventing the wheel with no good reason?
    Because it is fun and educational ! ...and can have a slight chance of producing a better wheel.
    (And...out of curiosity...can this be utilised to implement a better even driven model compared to callback functions?)

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by manasij7479 View Post
    Because it is fun and educational ! ...and can have a slight chance of producing a better wheel.
    Fun and educational, possibly.

    While there is always the possibility of some breakthrough what leads to a better wheel, it is more likelihood of recreating the square wheel and then expending a lot of effort trying to get it to rotate.

    Quote Originally Posted by manasij7479 View Post
    (And...out of curiosity...can this be utilised to implement a better even driven model compared to callback functions?)
    That depends on what you call "better". The implementation of debuggers amounts to one program setting up an environment that allows it to control and monitor the internal execution of another program. You could use the same techniques as an alternative to callbacks, I suppose.

    However, I have in mind images of sledgehammers and nuts.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doesn't consider change in variable
    By f0xy in forum C Programming
    Replies: 5
    Last Post: 10-29-2011, 08:33 AM
  2. Can't change variable from another function...
    By Ryan. in forum C Programming
    Replies: 9
    Last Post: 03-11-2011, 12:10 AM
  3. Replies: 4
    Last Post: 02-28-2011, 07:57 AM
  4. change variable and repeat
    By altf4thc in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2010, 05:27 PM
  5. unwanted Variable Change
    By purebuu in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 08:56 PM