Search code examples
c++stringdebugginggdbstdstring

Setting an std::string variable value from gdb?


When the debugger is stopped at a breakpoint, is it possible to modify the value of a std::string variable without resorting to hacks like tweaking the memory image of the current buffer?

E.g. something like

set var mystring="hello world"

Solution

  • Try this (tested and works for me):

    call mystring.assign("hello world")
    

    The key is that instead of modifying memory directly, you call the object's functions to change its state. It so happens that std::basic_string has a member function called assign which does the job.