Home > front end >  Find when a particular place in memory is changed in c
Find when a particular place in memory is changed in c

Time:08-09

Let's say I have an object MyObject and it has an attribute double MyObject::a. When I initialize a I set it to 0.05, but at some point when I run my code I notice that a is 1.something e-316, even though my code is never supposed to change its value. If I knew an exact place in memory that a occupies, is there a tool that will tell me when exactly this place is overwritten?

CodePudding user response:

As others have already noted, you could use a debugger for this. Most reasonably recent debuggers have some capability to break when a value is written to a particular variable (e.g., Visual Studio calls this a "watchpoint" or "data breakpoint" (depending on what age of IDE you're looking at), if memory serves.

Depending on the situation, you might be able to get some useful information by changing your double to a const double:

const double value { 0.05 };

Then any code that tries to assign a new value to this variable simply won't compile.

If, however, the problem arises from some code doing an out of bounds write, rather than assigning to the value that's getting overwritten, this won't help find that.

gdb watchpoints
VS data breakpoints

  • Related