如何更改gdb中的值 [英] How do I change values in gdb

查看:52
本文介绍了如何更改gdb中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有用于家庭作业的这段代码,必须使用gdb进行调试.我发现了问题,但不知道如何使用gdb进行更改

So I have this code for homework that I must debug with gdb. I have found the problem but don't know how to use gdb to change it

#define ARRAYSIZE 12
for (i = ARRAYSIZE - 2; i > 0; i--) {
    for (j = i; j < ARRAYSIZE - 1; j++) {

我知道问题的解决方案是这样:

I know that the solution to the problem is this:

for (i = ARRAYSIZE; i > 0; i--) {
    for (j = i-1; j < ARRAYSIZE - 1; j++) {

但是不知道如何在for循环中更改参数.我已经尝试过这种方法,但是它不起作用:

But don't know how to change the parameters in the for loop. I've tried it this way but it doesn't work:

81        for (i = ARRAYSIZE - 2; i > 0; i--) {
(gdb) set var i=12
(gdb) p i
$1 = 12
(gdb) n
82          for (j = i; j < ARRAYSIZE - 1; j++) {
(gdb) set var j=i-1
(gdb) p j
$2 = 9
(gdb) p i
$3 = 10

我还尝试使用 print i = 12 print j = i-1 更改值,但结果相同.

I've also tried using print i=12 and print j=i-1 to change the values but the result is the same.

推荐答案

您的问题是for循环的 i = ARRAYSIZE-2 部分尚未执行.

Your issue is that the i = ARRAYSIZE - 2 part of the for loop havn't executed yet.

因此,您要将 i 设置为 12 ,下一个要执行的操作是 i = ARRAYSIZE-2 设置了 i到10.

So you're setting i to 12 and the next thing that's executed is i = ARRAYSIZE - 2 that sets i to 10.

我不知道通过哪种简单的方法来遍历for循环的不同表现形式,但是您可以使用以下方法更改 i :一旦到达下一个源代码行,就 set var i = 10 .

I don't know any easy way to step through the different exprssions of a for loop, but you can change i with e.g. set var i = 10 once you reach the next source code line.

这篇关于如何更改gdb中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆