1

我的任务是调试教授写的程序。我们必须在 putty 中完成所有这些工作,而且我是 bash shell 脚本的新手(阅读:我对函数和处理 C++ 程序一无所知)。

所以我编译了 untitled.cpp 文件,它使程序无标题。然后我使用 ./untitled 执行程序,使其运行。目前很好。程序显示“种子值?”我根据分配输入给定值。然后,由于程序结束,命令提示符回到untitled目录。我不确定下一步该做什么,因为这就是我所有的问题所在。

这是说明:

运行程序(单步),输入种子值3222011。调用shuffle()函数后,array[8]的值是多少?

免责声明:我不是在要求答案,只是如何找到它们。在我刚才提到的问题之后还有 7 个问题。

我的问题:

  1. 如何找到数组元素值?或者任何数据成员的值,真的吗?
  2. 后来它说“您可以通过键入 gdb 命令打印数组在 main() 函数中打印整个数组的内容。” 这不起作用。它仅在当前上下文中返回“无符号”数组。
  3. “步入”函数是什么意思,我该怎么做?

给定的程序是:


#include <cstdlib>
#include <iostream>

using std::cout;
using std::cin;
using std::flush;

const int ARRAYSIZE = 30;

void fill(int ar[], int size);
void shuffle(int ar[], int size);

int main(void)
{

  int array[ARRAYSIZE] = {0}; // Clear out array
  int seed;

  cout << "Seed value? " << flush;
  cin >> seed;
  srand(seed);

  fill(array, ARRAYSIZE);

  shuffle(array, ARRAYSIZE);

  return 0;
}

void fill(int b[], int size)
{
  int index;

  // Place random values at random locations in the array
  for(int i = 0; i < 10 * size; i++)
    {
      index = rand() % size;
      b[index] = rand();
    }
}

void shuffle(int ar [], int size)
{
  int* first, * second;

  for(int j = 0; j < 5 * size; j++)
    {
      // Pick a random pair of positions to swap
      first  = ar + rand() % size;
      second = ar + rand() % size;

      // Swap
      int temp = *first;
      *first = *second;
      *second = temp;
    }
}

4

2 回答 2

1

我几乎在这里给你答案。但是你应该学习如何使用 gdb。

你的老师正在测试这些东西——你是否知道如何运行 gdb——你是否知道如何附加断点——你是否知道在使用 gdb 调试程序时如何打印一个值

您使用 --ggdb 选项编译程序,例如

g++ -ggdb untitled.cpp

然后这就是您将 gdb 附加到程序的方式

gdb a.out

完成此操作后,您将进入 gdb 命令提示符。您的程序尚未启动。在你启动程序之前,你需要像这样创建一个“断点”

b 27

这在 untitled.cpp 的第 27 行创建了断点

然后输入 r 运行程序

r

输入您的种子值,然后按 Enter。您将看到 gdb 将您带到您的断点处,即程序的第 27 行。现在您可以打印您想要查看的值。

以下是步骤

snegi@snegi-p7-1267c:~/Workspace/scripts$ gdb a.out 
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/satyarth/Workspace/scripts/a.out...done.
(gdb) b 27
Breakpoint 1 at 0x40094c: file untitled.cpp, line 27.
(gdb) run
Starting program: /home/satyarth/Workspace/scripts/a.out 
Seed value? 3222011

Breakpoint 1, main () at untitled.cpp:27
27    return 0;
(gdb) p array[8]
$1 = 560529867
于 2013-10-28T23:58:55.127 回答
0

Stack Overflow typically is a one question per post forum, however you've put in good effort in framing the question hence would like to point you in the right direction.

Do I do these on the same line as the program or do I do them after executing the file? Basically, where and how do I call the shuffle function?

After calling the shuffle() function, what is the value of array[8]?

You don't have to call the shuffle function. Its already called in the main program. The instructor is asking you to evaluate value of the array after its called in main.


Here is a GDB tutorial for beginners that should answer the rest of your questions. Beej's Quick Guide to GDB

于 2013-10-28T23:51:57.860 回答