c - How do I track variable changes in gcc -
is possible track when , how variable change? coding in c using gcc on linux
if want put code inside program run when variable changes, no - standard c doesn't provide way that. have find places in program try change variable value , put logging code in each , every one. can make more reliable renaming variable fixing points compilation breaks, works if can recompile "client" code using variable: if variable in library lots of other peoples' applications using it, might not practical.
using get/set functions access...
it's idea not write code directly uses variable, instead provide functions , set value: can put checks or logging inside functions if becomes useful day. but, don't want everywhere or code verbose , slower run.
polling checks changes variable...
if it's enough check every , then, can write code triggered timer/alarm signal, or runs in thread, checks on variable periodically see if it's changed, won't find out how changed, , if value changes changed might miss changes completely.
using c++
if compile program under c++ (if possible, small programs may not require modifications code), can perhaps change type of data item in question , write overloaded operator=()
function called when variable assigned to, , operator
old-type()
conversion function places variable used won't have modified:
template <typename t> class intercept { intercept(const t& t) : t_(t) { } t& operator=(const t& t) { std::cerr << "change " << t_ << " " << t << '\n'; t_ = t; return *this; } operator t&() { return t_; } operator const t&() const { return t_; } t t_; };
then change e.g.
int x; // intercept<int> x; //
debuggers
as drakosha says, if debugging issue can run program under debugger until resolve issue.
Comments
Post a Comment