3

I know that I can output the value of a variable using

(gdb) p var_name
$1 = "varvalue"

Is there a way to set the value of a variable while debugging with gdb? The document that I'm using doesn't seem to have this command.

Edit

I've since learned that I can change the state of objects in memory by simply calling their functions! So to modify an object of type std::string, just call the assign function as follows:

(gdb) call str.assign("New Value")

Thank you.

2 Answers 2

6

Google is your friend:

For example, if your program has a variable width, you get an error if you try to set a new value with just `set width=13', because GDB has the command set width:

(gdb) whatis width  
type = double   
(gdb) p width $4 = 13  
(gdb) set width=47  
Invalid syntax in expression. 

The invalid expression, of course, is `=47'. In order to actually set the program's variable width, use

(gdb) set var width=47

You can find the needed information in the following document:

3
2

A little bit later but to set a string variable you should try this>

gdb set var string_variable = 'new_string_variable'

Character numbers must be the same for the two variable definitions, in order to apply the change as appropriate. Quotes need to be applied to new variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.