How to watch a variable in c

I have a codebase of almost 16000 lines (C code) in which I am performing different operations (on a Raspberry Pi). After each operation, I am updating the value of dc. dc is initially 0 and if through some error my controller loses its connection with my laptop it becomes 1.

I need to call a function whenever it goes 1. I heard of a function in JavaScript called Object.prototype.watch() and unwatch(). Basically what it does is watches a variable and whenever its value changes, it calls a function. I need to implement a similar function or statement or anything that calls a function when my dc value changes.

I cannot use if-else after each updation of dc because it is not a good way of coding and there are going to be a lot of if-else if I use it.

4 answers

  • answered 2018-07-11 06:01 P__J__

    Nothing like this exists. Interpreted or managed languages have completely different rules. There is no other way than if.

    You can wrap it into some kind of assertion or function but there is no other way than if in that wrapper

  • answered 2018-07-11 06:09 visibleman

    This post explains how to set watch conditions using GDB (Debugger). This is of-course just possible in a dev environment, and not some check that you can do in live code.

    I think the only other way is to implement a macro or function named check_dc(), and call that after every update. Alternatively create a function update_dc() which performs both the update and the check.

  • answered 2018-07-11 06:15 klutt

    The sad answer is: No, there's no reliable way to watch a variable in C this way.

    Depending on how the code works there are some workarounds.

    One way is to hide (yes I know, it's hard to hide stuff completely in C) the variable. Define functions:

    int noConnection() { return _noConnection; }
    void lostConnection() { _noConnection = 1; myFunction(); }
    

    Another way is to code some monitor that checks the variable at regular intervals. A drawback is if you really need this function to be run every time it changes, because it will not catch the event that a variable is changed and changed back between checks.

  • answered 2018-07-11 06:19 phuclv

    If you know that the variable is not referenced by any pointers then you already know the places where it can be updated, so you can use visibleman's method.

    However in the general case when you don't know exactly at what point it can be updated, the easiest way is using a separate thread for watching it. You'll do some kind of polling by using a timer, or by checking the value and then sleep for some time to avoid wasting CPU usage.

    You may also run a debugger like gdb and attach it to your own process to watch. It'll be a lot more flexible this way, but you'll need more memory. You can try some debugging library if available

    Many architectures do have hardware watch points, so debuggers will try to use them up before turning up to software watching. If you know about the architecture you can also set up the debug registers to do like that