Saturday, April 13, 2013

Change the value of a std::string in gdb debugging

In gdb stepping, you can change the value of a variable by running:

   (gdb) set myvar=5
   to change the variable myvar to 5.

Because an std::string is not a primitive data type, it cannot be changed in this way.

The std::string is a class and it has methods that you can call in gdb to change the value of an instance of it. For example, if mystring is an object of std::string, you can do either:

   (gdb) call mystring.assign("new value")

   (gdb) call mystring.operator=("new value")
to change the value of mystring into "new value".

No comments:

Get This <