22

Is it possible... when the debugger is stopped at a breakpoint, 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"

?

SamB
  • 8,670
  • 5
  • 46
  • 53
Stabledog
  • 2,866
  • 1
  • 28
  • 38

1 Answers1

34

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.

Chris Jester-Young
  • 213,251
  • 44
  • 377
  • 423
  • 1
    Excellent! That's not only exactly what I need, it clarifies my thinking about how gdb works. – Stabledog Mar 23 '10 at 20:01
  • 2
    You can call the overloaded operators explicitly with `p mystring.operator=("hello world")` which is the verbose way of executing `mystring = "hello world"` and should work even when no other function overload exists. – Peter Oct 07 '20 at 12:12