How to Set a Watch Point for an Instance Variable?

My class is defined in a header file, and I need to watch for its private non-static member in GDB, like this:

class foo {
    int bar;
};
0

1 Answer

You can set watchpoint on memory address.

You should stop in your code somewhere after foo constructor execution and print bar variable address. Then you can set watchpoint on address like this:

(gdb) p &bar
$1 = (int *) 0x10793ad0
(gdb) watch *0x10793ad0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest